1

I have a list created like this: x <- c("a", "b", "c"), where a,b,c are characters and a value. I would like to create data.frames (with other operations or commands) that will be named based off of the list x. This will be a loop with lots of functions in it but the most important is that it can read the list as characters that then become the name of a data.frame in the next step.

Example: y is a dataframe that has a b and c listed as characters in a column called family

x <- c("a", "b", "c")

for (i in 1:length(x)) {
  eval(as.name(i) <- y[which(y$family == "(eval(as.name(i))"),]
}

I have tried eval(as.name(i), I have tried cat(i), I have tried cat[[i]], I have tried paste, print, or just [[i]]. Suggestions?

3
  • 1
    colnames(your_data_frame) <- x Commented Jun 8, 2018 at 21:20
  • This doesn't work because I'm looking to name the dataframe immediately not just change the column names. I'm looking to name several dataframes within a loop. Ie, result would be a loop that ends up with 3 dataframes with the names a, b and c. Commented Jun 8, 2018 at 21:30
  • Duplicate of R- how to dynamically name data frames?, which is in turn a dupe of this. Use assign or purrr Commented Jun 8, 2018 at 22:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.