I would like to construct a dataframe row-by-row in R. I\'ve done some searching, and all I came up with is the suggestion to create an empty list, keep a list index scalar,
I've found this way to create dataframe by raw without matrix.
With automatic column name
df<-data.frame(
t(data.frame(c(1,"a",100),c(2,"b",200),c(3,"c",300)))
,row.names = NULL,stringsAsFactors = FALSE
)
With column name
df<-setNames(
data.frame(
t(data.frame(c(1,"a",100),c(2,"b",200),c(3,"c",300)))
,row.names = NULL,stringsAsFactors = FALSE
),
c("col1","col2","col3")
)
You can grow them row by row by appending or using rbind().
That does not mean you should. Dynamically growing structures is one of the least efficient ways to code in R.
If you can, allocate your entire data.frame up front:
N <- 1e4 # total number of rows to preallocate--possibly an overestimate
DF <- data.frame(num=rep(NA, N), txt=rep("", N), # as many cols as you need
stringsAsFactors=FALSE) # you don't know levels yet
and then during your operations insert row at a time
DF[i, ] <- list(1.4, "foo")
That should work for arbitrary data.frame and be much more efficient. If you overshot N you can always shrink empty rows out at the end.