I would like to build a data frame in a loop adding a new column each time using cbind. I try the following:
test <- NULL
df <- data.frame(x=c(1,2,3,4))
test <- cbind(test, df)
This generates an error:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 4
What is the correct way to instantiate a blank data frame in R and then bind to it in a loop?
Thanks
You need to create
testas a structure that has the same number of rows so thatcbind.data.framewill not throw an error:Two other methods: