So let's say I'm trying to make a data frame(df2) that has value of twice as big as another data frame(df1). So the two data frames have exactly the same columns. Also, let's say df1 has 10 objects. Here is my code.
library(data.table)
for (i in (1:10) {
id <- df1$ID[i]
newAttr1 <- df1$attr1[i] * 2
newAttr2 <- df1$attr2[i] * 2
newAttr3 <- df1$attr3[i] * 2
NewRow <- list(id, newAttr1, newAttr2, newAttr3)
rbindlist(list(df2, NewRow))
}
I thought this should work, but somehow there is NO objects in df2. What is the problem?
Thanks a lot in advance :)
d2<- rbindlist(list(df2, NewRow))forloop for this.df1$attr1 * 2should work directly?attr1but usually yes. See for examplemtcars$mpg * 2. There could be a better way to solve your problem, we might be able to help if you could update with a reproducible example.