This question is related to this How to loop over the columns in a dataframe, apply spread, and create a new dataframe in R?
I have a list with 3 tibbles:
$Peter
# A tibble: 3 x 4
date Peter Ben Mary
<chr> <dbl> <dbl> <dbl>
1 2020-03-30 0.4 NA NA
2 2020-10-14 NA 0.6 NA
3 2020-12-06 NA NA 0.7
$Ben
# A tibble: 3 x 4
date Peter Ben Mary
<chr> <dbl> <dbl> <dbl>
1 2020-03-30 0.5 NA NA
2 2020-10-14 NA 0.4 NA
3 2020-12-06 NA NA 0.2
$Mary
# A tibble: 3 x 4
date Peter Ben Mary
<chr> <dbl> <dbl> <dbl>
1 2020-03-30 0.2 NA NA
2 2020-10-14 NA 0.1 NA
3 2020-12-06 NA NA 0.9
mylistdf <- list(Peter = structure(list(date = c("2020-03-30", "2020-10-14",
"2020-12-06"), Peter = c(0.4, NA, NA), Ben = c(NA, 0.6, NA),
Mary = c(NA, NA, 0.7)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame")), Ben = structure(list(date = c("2020-03-30",
"2020-10-14", "2020-12-06"), Peter = c(0.5, NA, NA), Ben = c(NA,
0.4, NA), Mary = c(NA, NA, 0.2)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame")), Mary = structure(list(date = c("2020-03-30",
"2020-10-14", "2020-12-06"), Peter = c(0.2, NA, NA), Ben = c(NA,
0.1, NA), Mary = c(NA, NA, 0.9)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame")))
How can I save all this 3 tibbles as an own tibble in the global environment:
Expected output would be kind of having Peter, Ben, Mary as tibbles or dataframes in the global environment.
I know we could do:
Peter <- mylistdf$Peter
Ben <- mylistdf$Ben
Mary <- mylistdf$Mary
But I whish to do it in one run with map or similar like:
mylistdf %>%
.....
CodePudding user response:
You can do this:
list2env(mylistdf,envir = .GlobalEnv)
but it is often recommended to keep your frames/tibbles in the original list.
CodePudding user response:
We could use %=%
library(collapse)
names(mylistdf) %=% mylistdf
-checking
> Peter
# A tibble: 3 × 4
date Peter Ben Mary
<chr> <dbl> <dbl> <dbl>
1 2020-03-30 0.4 NA NA
2 2020-10-14 NA 0.6 NA
3 2020-12-06 NA NA 0.7
> Mary
# A tibble: 3 × 4
date Peter Ben Mary
<chr> <dbl> <dbl> <dbl>
1 2020-03-30 0.2 NA NA
2 2020-10-14 NA 0.1 NA
3 2020-12-06 NA NA 0.9