I am creating multiple data frames inside a loop using assign, but once they have been created, how do I modify them inside the same loop?
For example the following code...
for(i in 1:3) {
assign(paste0("df", i),data.frame(A=c('a','b','c'),B=c("","","")))
}
create three dataframe, namely df1,df2, and df3 each looking like this...
A B
1 a
2 b
3 c
With the B column blank.
Desired output is to have the value of 'i' inside the B column while creating df[i]. So df2 would be...
A B
1 a 2
2 b 2
3 c 2
Important: Note that while I could do that inside the assign command itself in this case, in the larger problem that I am working on, I need to do the assignment outside the assign command as the dataframe I am creating is actually a subset of a larger dataframe not a new dataframe itself.
I have tried...
for(i in 1:3) {
assign(paste0("df", i),data.frame(A=c('a','b','c'),B=c("","","")))
paste0("df", i)$B <- i
}
... which doesn't work. What can work in place of paste0("df", i)$B <- i?