Unlist a list of dataframes
Use list2env
it is specially designed for this:
From a named list x, create an environment containing all list
components as objects, or “multi-assign” from x into a pre-existing
environment.
So here :
list2env(mylist ,.GlobalEnv)
How to unlist a list of data frames into a single data frame in R
In base R:
lst2 <- lapply(lst,function(x) cbind(rowname=rownames(x),x))
df1 <- Reduce(function(x,y) merge(x,y,all=T),lst2)
rownames(df1) <- df1[[1]]
df1 <- df1[-1]
df1[is.na(df1)] <- 0
df1
# x y z w r s
# a 1 2 0 0 0 0
# b 2 3 0 0 2 3
# c 3 4 1 2 3 4
# d 0 0 2 3 4 5
# e 0 0 3 4 0 0
# f 0 0 4 5 0 0
tidyverse
can make things more compact/readable:
library(tidyverse)
lst %>%
map(rownames_to_column) %>%
reduce(full_join) %>%
`[<-`(is.na(.),value=0) %>%
column_to_rownames
# x y z w r s
# a 1 2 0 0 0 0
# b 2 3 0 0 2 3
# c 3 4 1 2 3 4
# d 0 0 2 3 4 5
# e 0 0 3 4 0 0
# f 0 0 4 5 0 0
merging directly by row names
merge
supports merging by row names if you set the argument by
to "row.names"
or to 0
, but strangely it returns a dataframe with a column Row.names
and no actual row name. This makes the Reduce
call much less smooth that it could have been, so in the end it's not much better, possibly worse, than my original base solution:
df1 <- Reduce(function(x,y) {
z <- merge(x,y,all=T,by=0)
rownames(z) <- z[[1]]
z[-1]},
lst)
df1[is.na(df1)] <- 0
How to unlist dataframes within a list into several dataframes itself?
Make the list based on names (and it will be a named list this way)
data = mget(ls(pattern = "mat\\d+")) ## use a regex pattern to create a named list
data = lapply(data, as_tibble) ## convert the matrices to tibbles
names(data) = paste0(names(data), ".done") ## modify the names
list2env(data, envir = .GlobalEnv) ## put them back in global environment
Read more advice on this at How do I make a list of data frames?
Unlist list of data frames to one data frame with change structure
library(tidyverse)
names(df) <- paste0('outcome', year)
df %>%
purrr::map_df(as.data.frame, .id = 'name') %>%
tidyr::pivot_wider(names_from = name, values_from = outcome) %>%
dplyr::arrange(id)
# A tibble: 20 x 12
id outcome1950 outcome1951 outcome1952 outcome1953 outcome1954 outcome1955 outcome1956 outcome1957 outcome1958 outcome1959 outcome1960
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 NA 0 NA 0 NA 0 NA NA 0 NA NA
2 2 1 NA 0 1 NA 1 NA 1 NA 0 NA
3 3 1 0 NA NA 1 NA NA 0 0 NA NA
4 4 0 0 NA NA NA 0 NA 1 0 1 NA
5 5 0 NA 0 NA NA NA 1 NA NA NA NA
6 6 0 NA 0 1 NA 1 1 NA 0 1 NA
7 7 NA 1 0 0 1 NA 1 0 NA NA 1
8 8 NA 0 0 0 1 NA NA 1 1 0 0
9 9 NA 0 0 NA 1 NA NA 1 NA 0 0
10 10 0 0 0 NA NA 0 1 NA NA 1 1
11 11 0 NA NA 1 NA NA 1 NA 1 0 NA
12 12 NA NA NA NA 0 1 NA NA 1 0 0
13 13 NA NA 0 NA 1 NA NA 1 0 1 0
14 14 0 1 NA NA 0 NA 0 NA NA 0 0
15 15 1 NA 1 0 NA 0 1 NA 1 NA NA
16 16 NA NA NA 0 0 0 NA NA 0 NA 0
17 17 NA NA NA NA NA NA NA 1 NA NA NA
18 18 NA NA NA NA NA NA 1 NA NA NA 1
19 19 1 0 NA 0 0 1 0 1 NA NA NA
20 20 NA 1 1 1 0 0 0 1 NA NA 0
How to unlist a list in dataframe column?
If in column are lists deduplicated with dict.fromkeys
and then join by whitespace:
#if values are strings
#z1['codes'] = z1['codes'].str.strip('[]').str.split(',\s*')
z1['codes'] = z1['codes'].apply(lambda x: ' '.join(dict.fromkeys(x).keys()))
print (z1)
codes
0 K70 X090a2 T8a981
1 A70 X90a2 T8a91
2 B70 X09a2 T8a81
3 C70 X00a2 T8981
Unlist a list file to multiple dataframes
You can use assign
to the global environment within an lapply
. Note I have wrapped the call in invisible
to avoid printing the output to the console.
invisible(lapply(names(L),function(x) assign(x,L[[x]],.GlobalEnv)))
ls()
[1] "a" "b" "c" "L"
a
X1 X2
1 1 3
2 2 4
However, while the elements are in the list, they might be easier to work with, using lapply
for example.
Unlist a list of dataframes in R
The error is a result of passing a list without names
list2env(list(1, 2, 3), .GlobalEnv)
Error in list2env(list(1, 2, 3), .GlobalEnv) : names(x) must be a
character vector of the same length as x
list2env(list(a= 1, b = 2, c = 3), .GlobalEnv)
<environment: R_GlobalEnv>
Unlist lists of dataframes and assign names on a loop - R
We can use get
to get the value of the data
for (i in May){
Date <- paste("2021-05", i, sep = "-")
List <- paste0("Data_", Date)
File <- get(List)[["df1"]]
DfName <- paste("df1", Date, sep = "_")
assign(DfName, File)
}
Another option is to use mget
to return all objects in a list
, loop over the list
with lapply
, extract the 'df1' (it is recommended to keep it in a list
instead of creating multiple objects), but if we need objects, use list2env
lst1 <- lapply(mget(paste0("Data_2021-05-", May)), `[[`, "df1")
names(lst1) <- paste0("df1", names(lst1))
list2env(lst1, .GlobalEnv)
Unlist a list within a list of data.frames into a single data.frame
We can try
library(tidyverse)
map_df(dat, bind_rows)
# ID Gender Phrase Phrase2
#1 1 Male Hello Goodbye
#2 1 Female Good afternoon Goodbye
#3 2 Male Hello Good afternoon
#4 2 Female Goodbye Goodbye
Related Topics
Long/Bigint/Decimal Equivalent Datatype in R
Table of Interactions - Case With Pets and Houses
Tools For Making Latex Tables in R
Using Data.Table Package Inside My Own Package
Select Groups With More Than One Distinct Value
Starting Shiny App After Password Input
How to Format a Number as Percentage in R
Pasting Two Vectors With Combinations of All Vectors' Elements
Calculate Cumulative Sum (Cumsum) by Group
Getting Warning: " 'Newdata' Had 1 Row But Variables Found Have 32 Rows" on Predict.Lm
Assign Multiple Columns Using := in Data.Table, by Group
Capitalize the First Letter of Both Words in a Two Word String
Don't Drop Zero Count: Dodged Barplot
Replace All Particular Values in a Data Frame