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?

smci
  • 32,567
  • 20
  • 113
  • 146
MGru
  • 35
  • 2
  • 7
  • 1
    `colnames(your_data_frame) <- x` – Alexis Jun 08 '18 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. – MGru Jun 08 '18 at 21:30
  • **Duplicate of [R- how to dynamically name data frames?](https://stackoverflow.com/questions/17255851/r-how-to-dynamically-name-data-frames)**, which is in turn a dupe of [this](https://stackoverflow.com/questions/14081982/how-to-assign-values-to-dynamic-names-variables-in-r). Use `assign` or `purrr` – smci Jun 08 '18 at 22:09

0 Answers0