2

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.

3
  • 3
    list2 = lapply(list2, cbind, df) is equivalent to your "I know how to do it one at a time" code. Not really sure how you want the rest of list1 to be used... Do you want cbind(vc, df), cbind(vc1, df1), cbind(vc2, df2)? In that case, Map(cbind, list2, list1) Commented Mar 3, 2020 at 15:21
  • Why have three dfs and only use df? Commented Mar 3, 2020 at 15:23
  • Because I have multiple df (df1, df2, df3.....). I would like to do the same thing as I do with df and vc, vc1, vc2 . And I have multiple logical vector for each df Commented Mar 3, 2020 at 15:28

3 Answers 3

2

An option with bind_cols and map

library(purrr)
map(list2, ~ bind_cols(l = .x, df))
Sign up to request clarification or add additional context in comments.

Comments

2

You can use lapply here :

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

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)

lapply(list2, function(l) cbind(l, df))
#> [[1]]
#>       l 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]]
#>       l 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]]
#>      l 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

Comments

2

If you intended to use all objects you provided: Put vectors and data frames into lists and cbind them one by one using Map.

Map(cbind, vc=list(vc, vc1, vc2), list(df, df1, df2))
# [[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]]
#      vc 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]]
#     vc 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.