Home > OS >  How I can get the tibbles generated by lapply as a seperate dataframe in R
How I can get the tibbles generated by lapply as a seperate dataframe in R

Time:03-10

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

CodePudding user response:

# 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]])  
  • Related