Reputation: 87
I have a for loop as
for(i in c("a","b","c","d"))
{
as.name(paste("df",i,sep=""))= mydataframe
}
mydataframe
is a data frame and I want to create data frames dfa
,dfb
,dfc
and dfd
using this loop.
The as.name(paste("df",i,sep=""))
does not work here. I do not want to create a list that has the 4 data frames.
Can I directly create 4 data frames from this loop?
Upvotes: 0
Views: 59
Reputation: 531
You can do this using assign
. Although in general, you are better off using lists.
Using your example:
for(i in letters[1:4]){
assign(paste0("df", i), mydataframe)
}
Note that this will simply create the same object 4 times, unless you change what mydataframe
is inside the loop.
Upvotes: 1