0

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?

3

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.