Merge multiple data frames in different list

Alexis Begni

I want to merge one df, with multiple logical vector. here's an example of what I want:

a <- c(1, 2, 3, 4, 5)
b <- c(1.1, 5.4, 9.3, 6.1 ,4.5)
df <- data.frame(cbind(a,b))
df1 <- data.frame(cbind(a,b))
df2 <- data.frame(cbind(a,b))
list1 <- list(df, df1, df2)

In my data, df1 and df2 are different than df but it's easier. I have my logical vector and I want to cbind them with the first df of the first list.

vc <- c(TRUE, FALSE, TRUE, TRUE,FALSE)
vc1 <- c(FALSE, FALSE, TRUE, TRUE, FALSE)
vc2 <- c(TRUE, TRUE, TRUE, TRUE, TRUE)
list2 <- list(vc, vc1, vc2)

I know how to do it one at a time.

list2[[1]] <- cbind(vc, df)
list2[[2]] <- cbind(vc1, df)
list2[[3]] <- cbind(vc2, df)

So the final result will be:

list2
[[1]]
     vc a   b
1  TRUE 1 1.1
2 FALSE 2 5.4
3  TRUE 3 9.3
4  TRUE 4 6.1
5 FALSE 5 4.5

[[2]]
    vc1 a   b
1 FALSE 1 1.1
2 FALSE 2 5.4
3  TRUE 3 9.3
4  TRUE 4 6.1
5 FALSE 5 4.5

[[3]]
   vc2 a   b
1 TRUE 1 1.1
2 TRUE 2 5.4
3 TRUE 3 9.3
4 TRUE 4 6.1
5 TRUE 5 4.5

But I really have a lot of data and it will take me "years" to do it.

akrun

An option with bind_cols and map

library(purrr)
map(list2, ~ bind_cols(l = .x, df))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Simultaneously merge multiple data.frames in a list

merge multiple data.frames [r]

Merge Two Data Frames to match date (different headings) with different lengths

merge list of data frames by different ids

rbind a list of data frames with different columns

merge data from multiple data frames on multiple conditions

Merge multiple data frames in a specific order R

Merge data frames on multiple common values

Merge 2 Different Data Frames - Python 3.6

Merge data frames based on column with different rows

How to read a single .csv file that consists of multiple commented data frames of different lengths as a list?

How to merge a data frame column with multiple data frames within a list?

Add columns to multiple data frames in the list with same column names but different values

Merge Data Frames of different years

labeling dynamically for different list of data frames

Different results applying merge in R over individual data frames and over one list of data frames

Need to merge multiple data frames by multiple common columns

Merge all possible combinations of multiple data frames

Merge data frames within a list

Merge two data frames on multiple values

Merge on pandas data frames with multiple values

Mapping different args to a list of data frames

Merge 2 pandas data frames on multiple columns

Merge data frames in a list by condition in purrr

Loop R merge multiple data frames together

Merge 2 data frames with multiple conditions

Writing multiple data frames from a list into excel, split 1 factor across different sheets and order by another factor

Merge a list of identical data frames that have missing values in different places

How to unlist a list of multiple tibbles into different data frames and rename them?