-1

I have generated a loop using lapply. When I run the loop I get the tibbles like like this these two examples, but to save the space, I didn't address 10 tibbles here

 Color  Variables   value
    Yellow  A   12
    Red B   11
    Blue    B   4
    …   …   …
    Color   Variables   value
    Red R   6
    Black   N   8
    Green       10

So I have 10 tibbles consecutively which have been generated by the lapply.

Now, I want to get a data frame for each. So I will have 10 data frames, like df1,df2,df3,...

The outcome for the first tibble is: df1

Color   Variables   value
Yellow  A   12
Red B   11
Blue    B   4

Or for the second table is :

df2

Color   Variables   value
Red R   6
Black   N   8
Green       10

So I get dfs, df1... df10

user330
  • 1,256
  • 1
  • 7
  • 12
  • 3
    `lapply(your_list_of_tibbles, as.data.frame)` will convert your list of tibbles to a list of data frames. Maybe have a look at [How to make a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207) which will give good context. You can use `list2env()` to put those list items in the global environment, but the linked answer will also explain why that's almost always a bad idea. – Gregor Thomas Mar 09 '22 at 15:06
  • Thank you, sorry it was not converted to a list of the data frames with a lack of errors. You get all columns separately. For example, for the first tibble, I want to have a data frame (df1) which is included all variables, and so on, df1,df2,... – user330 Mar 09 '22 at 15:20
  • Please make your example reproducible, providing copy/pasteable sample input and desired output. Use `dput()` to share a subset of your data. – Gregor Thomas Mar 09 '22 at 15:25
  • I have updated the question, So I want to get 10 data frames like df1 – user330 Mar 09 '22 at 15:27
  • Gregor Thomas- it's ok your downvoted, but you don't need to make a comment followed by downvoted. I am trying my best to clarify the question – user330 Mar 09 '22 at 15:30
  • This is not clear. Please use `dput()` to share data so it is copy/pasteable and includes class and structure information. It's not at all clear what is a list, what is a tibble, and what is a data frame. The structure of your input is also not clear. Please use `dput()` to make it clear. If you need more advice on making a reproducible example in R, please see the FAQ [How to make a reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Gregor Thomas Mar 09 '22 at 15:31
  • I have a large database, you don't need to have my data if you are familiar with lapply loop. They generate the same outcome. Which I have provided, sorry. – user330 Mar 09 '22 at 15:34
  • 1
    Generally on the site [it is encouraged to always comment when you downvote](https://meta.stackoverflow.com/q/357436/903061). I don't downvote new users when they aren't providing reproducible examples, but you have asked many questions in the R tag. I do not want your whole large database. I want a small, minimal, reproducible example. `lapply` produces a list, that's what the `l` stands for. Nothing you show looks like a list. So it is very confusing. I will stop looking at the question as you do not want to follow my suggestions. But I think others will also be confused. – Gregor Thomas Mar 09 '22 at 15:36

1 Answers1

1
# create a list of tibbles and store them in a list
dfs <- lapply(1:3, function(i) {
  tibble(x = LETTERS[i], y = i)
})

# you can simply access them by index
df1 <- dfs[[1]]

# if you truly want to create all df objects
for (i in seq(dfs))
  assign(paste0("df", i), dfs[[i]])  
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22