I would like to do a cbind with two data.frame objects. However the first one is empty, just created for loop iteration. I would like to know if it is possible.
When I try:
data <- data.frame()
data1 <- cbind(data, mpg)
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 0, 234
I am not successful.
My loop is a little longer, but in general, I have about 100 files in which I need to create the taxa_col_name variable and I need to add the data.frame generated in the loop to a data.frame that will be my final base.
data <- data.frame()
for (i in 1:100) {
rate_col_name <- sprintf("rate_%s", i)
data1 <- data1 %>%
mutate(!!rate_col_name:=
(population_2000)*((population_2010/population_2000_previous)))
data1$population_2010 = NULL
data1$population_2000_previous <- NULL
rm(pop_2000_previous, pop_2010)
gc()
date <- cbind(date, date1)
}
This happens when you attempt to
cbindthe existing but emptydatawithdata1, which actually has rows and presumably one column. That's because for cbind, the two data frames need to have the same nunmber of rows. A minimum example to produce that error is:For you, this is happening at the end of the first iteration of your loop. Here's a shorter version of that:
One solution is to initialize
datawith actual data in the first iteration of the loop:One could also do the first iteration "manually" outside the loop and then start the loop at 2 instead of 1:
Another solution is to start with a
listand turn that into adata.framein the end: