Data Frames

  • Data frames are lists of vectors
  • So copying and modifying a column only affects that column
  • BUT if you modify a row, every column must be copied
d1 <- data.frame(x = c(1, 5, 6), y = c(2, 4, 3))
d2 <- d1
d3 <- d1

Only the modified column changes

d2[, 2] <- d2[, 2] * 2
ref(d1, d2)
#> █ [1:0x560e52195088] <df[,2]> 
#> ├─x = [2:0x560e513fe918] <dbl> 
#> └─y = [3:0x560e513fe968] <dbl> 
#>  
#> █ [4:0x560e51dfbca8] <df[,2]> 
#> ├─x = [2:0x560e513fe918] 
#> └─y = [5:0x560e51377178] <dbl>

All columns change

d3[1, ] <- d3[1, ] * 3
ref(d1, d3)
#> █ [1:0x560e52195088] <df[,2]> 
#> ├─x = [2:0x560e513fe918] <dbl> 
#> └─y = [3:0x560e513fe968] <dbl> 
#>  
#> █ [4:0x560e5389a578] <df[,2]> 
#> ├─x = [5:0x560e53162798] <dbl> 
#> └─y = [6:0x560e53162748] <dbl>