Combine a list of data frames into one data frame by row
Use bind_rows()
from the dplyr package:
bind_rows(list_of_dataframes, .id = "column_label")
Split a list into separate data frame in R
We can use imap
to get the names and then use set_names
library(purrr)
library(dplyr)
library(stringr)
imap(list_a, ~ set_names(tibble(.x), .y)) %>%
set_names(str_c("DF", 1:3)) %>%
list2env(.GlobalEnv)
DF1
# A tibble: 1 x 1
# Banana
# <dbl>
#1 8.7
DF2
# A tibble: 1 x 1
# Strawberry
# <dbl>
#1 2.3
DF3
# A tibble: 1 x 1
# Apple
# <dbl>
#1 3.5
If we need separate columns
library(tibble)
enframe(list_a) %>%
unnest(c(value)) %>%
group_split(rn = row_number(), keep = FALSE) %>%
set_names(str_c("DF", 1:3)) %>%
list2env(.GlobalEnv)
DF1
# A tibble: 1 x 2
# name value
# <chr> <dbl>
#1 Banana 8.7
DF2
# A tibble: 1 x 2
# name value
# <chr> <dbl>
#1 Strawberry 2.3
DF3
# A tibble: 1 x 2
# name value
# <chr> <dbl>
#1 Apple 3.5
Combining a list of data frames into a new data frame in R
Note that in your list of dataframes (df_list
) all the columns have different names (Area1
, Area2
, Area3
) whereas in your output dataframe they all have been combined into one single column. So for that you need to change the different column names to the same one and bind the dataframes together.
library(dplyr)
library(purrr)
result <- map_df(df_list, ~.x %>%
rename_with(~"Area", contains('Area')), .id = 'FileName')
result
# FileName Area
#1 a1_areaX 100
#2 a2_areaX 200
#3 a3_areaX 300
converting list of data frames into single data frame
I am sure there is a better solution out there.
The problem you can't use rbind
is because you have differing colnames (e.g. T1, T2, T3...)
So my idea was to name each df in the list with
names(table_list1) <- banner
Map(cbind, table_list1, SampleID = names(table_list1))
then add new names to each dataframe:
colnames <- c("name","Median","Mean", "N")
for (i in seq_along(table_list1)){
colnames(table_list1[[i]]) <- colnames
}
Now you can use your code:
t2 <- do.call(rbind,table_list1)
t2
func1<-function(db,list_var,var_name_list,....){
table_list1<-list()
for (d in 1:length(df_list)) {
table_list<-list()
for (i in 1:length(list_var)) {
table_list[[i]]<-sub_fun(db, list_var[i],var_name_list[i])
t1 <- do.call(rbind,table_list)
}
colnames(t1)[1] <- banner[[d]]
t1 <- t1 %>%
add_row() %>%
mutate_all(~replace(., is.na(.), ""))
table_list1[[d]] <- t1
}
names(table_list1) <- banner
Map(cbind, table_list1, SampleID = names(table_list1))
colnames <- c("name","Median","Mean", "N")
for (i in seq_along(table_list1)){
colnames(table_list1[[i]]) <- colnames
}
t2 <- do.call(rbind,table_list1)
t2
}
name Median Mean N
T1.1 klick 6 6.1875 32
T1.2 Nemar 196.3 230.721875 32
T1.3 Wingo 123 146.6875 32
T1.4
T2.1 klick 6 6.1875 32
T2.2 Nemar 196.3 230.721875 32
T2.3 Wingo 123 146.6875 32
T2.4
T3.1 klick 6 6.1875 32
T3.2 Nemar 196.3 230.721875 32
T3.3 Wingo 123 146.6875 32
T3.4
Converting a list of list of data.frames to a single data.frame in R
We can use lapply/Map
in base R
. We can loop through the list
with lapply
, rbind
the nested list
elements, then create a new column with Map
and rbind
the outer list
elements
out <- do.call(rbind, Map(cbind, lapply(L, function(x)
do.call(rbind, x)), id = seq_along(L)))
row.names(out) <- NULL
out
# d SD id
#1 1 3 1
#2 2 4 1
#3 2 1 2
#4 3 2 2
#5 7 6 2
#6 8 7 2
#7 5 3 3
#8 6 4 3
#9 8 1 3
#10 9 2 3
#11 4 6 3
#12 5 7 3
Based on the comments, if we need to add another column from the names
of the inner list
out1 <- do.call(rbind, Map(cbind, lapply(L, function(dat)
do.call(rbind, Map(cbind, dat, es.type = names(dat)))), id = seq_along(L)))
row.names(out1) <- NULL
out1
# d SD es.type id
#1 1 3 Short 1
#2 2 4 Short 1
#3 2 1 Short 2
#4 3 2 Short 2
#5 7 6 Long1 2
#6 8 7 Long1 2
#7 5 3 Short 3
#8 6 4 Short 3
#9 8 1 Long1 3
#10 9 2 Long1 3
#11 4 6 Long2 3
#12 5 7 Long2 3
If there are ..\\d+
and want to remove
out1 <- do.call(rbind, Map(cbind, lapply(L, function(dat)
do.call(rbind, Map(cbind, dat,
es.type = sub("\\.*\\d+$", "", names(dat))))), id = seq_along(L)))
row.names(out1) <- NULL
out1
# d SD es.type id
#1 1 3 Short 1
#2 2 4 Short 1
#3 2 1 Short 2
#4 3 2 Short 2
#5 7 6 Long 2
#6 8 7 Long 2
#7 5 3 Short 3
#8 6 4 Short 3
#9 8 1 Long 3
#10 9 2 Long 3
#11 4 6 Long 3
#12 5 7 Long 3
How can I convert a list of data frames into a data frame of lists using purrr or tidyr?
If I understand the output you want correctly, you can do
list %>% map_df(~.x %>% nest(cols=-ID))
Since nest
requires a data.frame, you need to map over the data frames in the list to do the mapping. Then map_df
will combine everything into one data.frame in the end.
Convert a list to a data frame
Update July 2020:
The default for the parameter stringsAsFactors
is now default.stringsAsFactors()
which in turn yields FALSE
as its default.
Assuming your list of lists is called l
:
df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))
The above will convert all character columns to factors, to avoid this you can add a parameter to the data.frame() call:
df <- data.frame(matrix(unlist(l), nrow=132, byrow=TRUE),stringsAsFactors=FALSE)
Related Topics
How to Define More Line Types for Graphs in R (Custom Linetype)
R Plotting Confidence Bands with Ggplot
Using Parallel's Parlapply: Unable to Access Variables Within Parallel Code
Import Data into R with an Unknown Number of Columns
Install.Packages Fails in Knitr Document: "Trying to Use Cran Without Setting a Mirror"
Divide Row Value by Aggregated Sum in R Data.Frame
Delete Duplicate Rows in Two Columns Simultaneously
Is There an R Function to Reshape This Data from Long to Wide
Differencebetween Cat and Print
How to Suppress the Vertical Gridlines in a Ggplot2 Plot
How to Delete Groups Containing Less Than 3 Rows of Data in R
How to Hold Figure Position with Figure Caption in PDF Output of Knitr
Use Filter in Dplyr Conditional on an If Statement in R
Split/Subset a Data Frame by Factors in One Column
R: How to Run Some Code on Load of Package
Dt: Dynamically Change Column Values Based on Selectinput from Another Column in R Shiny App
Convert a Character Vector of Mixed Numbers, Fractions, and Integers to Numeric