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")
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
Combine list of dataframes into one dataframe and summarize in one step
Based on the OP's code, different functions were used on different columns. So, we may have to individually apply those elementwise functions
library(purrr)
reduce(df_list, ~ tibble(.x[1], .x[2] + .y[2], pmax(.x[3], .y[3])))
-output
# A tibble: 3 × 3
Group Top_Group Efficiency
<chr> <int> <dbl>
1 A 1 0.465
2 B 2 0.652
3 C 0 0.755
Combine a dataframe and a list of dataframes into a one-dimensional list of dataframes, R
You can use c
but you have to cover your data.frame
a into a list.
res <- c(b, list(a=a))
str(res)
#List of 3
# $ c:'data.frame': 10 obs. of 1 variable:
# ..$ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
# $ d:'data.frame': 20 obs. of 2 variables:
# ..$ x: int [1:20] 1 2 3 4 5 6 7 8 9 10 ...
# ..$ y: int [1:20] 11 12 13 14 15 16 17 18 19 20 ...
# $ a:'data.frame': 10 obs. of 2 variables:
# ..$ xa: int [1:10] 1 2 3 4 5 6 7 8 9 10
# ..$ ya: int [1:10] 11 12 13 14 15 16 17 18 19 20
Can I combine a list of similar dataframes into a single dataframe?
do.call("rbind", foo)
should do the trick.
merge list of dataframes into one dataframe with id
dplyr::bind_rows
should do what you want (it binds list elements). You have to pass additional argument for .id
. To make this .id
you have to name list elements.
# Name list elements
names(converted_json) <- dataOriginal$participant.code
# bind rows and add .id
library(dplyr)
bind_rows(converted_json, .id = "partcode")
Transform list of data frames to one data frame with df names in extra column
We can use Map
to do this
do.call(rbind, unname(Map(cbind, colour = names(my.list), my.list)))
Result:
do.call(rbind, unname(Map(cbind, colour = names(my.list), my.list)))
colour x y
1 green 1.1 4.1
2 green 2.0 5.1
3 green 3.5 6.3
4 blue 3.3 6.4
5 blue 2.7 5.2
6 blue 1.9 2.4
7 blue 2.9 3.0
8 blue 1.9 5.1
9 yellow 3.4 6.2
10 yellow 2.7 5.0
11 yellow 1.8 4.1
12 yellow 0.2 0.5
Or if we are using dplyr
, then bind_rows
have the option .id
library(dplyr)
bind_rows(my.list, .id = 'colour')
join two lists of data frames into a single list of binded_rows data frames
We need map2
for binding two corresponding list
s
library(purrr)
map2_dfr(rapheys_df_list, XGB_models_Prep, bind_rows)
data
rapheys_df_list <- list(data.frame(col1 = 1:3, col2 = 4:6),
data.frame(col1 = 7:9, col2 = 10:12))
XGB_models_Prep <- list(data.frame(col1 = 2:5, col2 = 3:6),
data.frame(col1 = 4:6, col2 = 0:2))
Related Topics
How to Get Summary Statistics by Group
Ggplot'S Qplot Does Not Execute on Sourcing
How to Name Variables on the Fly
Why Does Data.Table Update Names(Dt) by Reference, Even If I Assign to Another Variable
Extracting Specific Columns from a Data Frame
Specify Custom Date Format For Colclasses Argument in Read.Table/Read.Csv
Selecting Only Duplicates Based on Multiple Columns in R
R: Rjava Package Install Failing
Subset/Filter Rows in a Data Frame Based on a Condition in a Column
How to Generate a Histogram for Each Column of My Table
How to Remove All Duplicates So That None Are Left in a Data Frame
Paste Multiple Columns Together
Using Reshape from Wide to Long in R
How to Change Language Settings in R
Ggplot Does Not Work If It Is Inside a For Loop Although It Works Outside of It