I am learning R and want to know the difference between populating an empty list and an empty data frame. My required dataset will have 3 variables, with 51 observations each.
Using empty list:
zscoremds <- list()
for(col in names(mds_numbers)) { 
  zscoremds[[col]] = zscore(mds_numbers[[col]]) 
}
mds_numbers is a 51x3 data frame with named columns; zscore is a function that calculates the z-score of each element in a column.
Using empty data frame:
zscoremds <- data.frame()
for (j in 1:3) {
  newcol <- zscore(mds_numbers[[j]])
  zscoremds <- cbind(zscoremds, newcol)
}
This does not work. I get an error "differing number of rows: 0,51"
However, when I pre-allocate the data frame to have 51 rows, it works:
zscoremds <- data.frame(matrix(nrow = 51, ncol = 0))                        
for (j in 1:3) {
  newcol <- zscore(mds_numbers[[j]])
  zscoremds <- cbind(zscoremds, newcol)
}
Why does it work on an empty list, but not on an empty data frame if I add new columns to it?
 
    