There are several issues here. Probably the one that is causing things not to work is that you are trying to access a column of the data frame data3
. To do that, you use the following data3[, i]
. Note the comma. That separates the row index from the column index.
Additionally, since you already know how big your data frame will be, allocate the space from the beginning:
tree.df <- data.frame(matrix(nrow = 241, ncol = 72))
tree.df
is already prepopulated with missing (NA
) values so you don't need to do it again. You can now rewrite your for loop as
for (i in colnames(data3)){
tree.df[, i] <- sample(data3[, i], 241, replace = TRUE)
}
Notice I spelled out TRUE
. This is better practice than using T
because T
can be reassigned. Compare:
T
T <- FALSE
T
TRUE <- FALSE