Home > database >  Add a column from one dataframe in a list to another dataframe in another list with map2
Add a column from one dataframe in a list to another dataframe in another list with map2

Time:01-13

I have two lists with different dataframes (original data has 70 dataframes for each list, totalising 2 million rows).

df1 <- data.frame(colA = LETTERS[1:5], colB = seq(1,5))
df2 <- data.frame(colA = LETTERS[6:10], colB = seq(6,10))
list1 <- list(df1,df2)
df3 <- data.frame(colA = LETTERS[1:5], colB = seq(11,15))
df4 <- data.frame(colA = LETTERS[6:10], colB = seq(16,20))
list2 <- list(df3,df4)

I want either to create a new list (e.g., desired_list) or to update the existing list1, adding colB from each dataframe of list2 (df3 and df4) naming the new column accordingly and re-naming the old ones. list1 and list2 have the same number of dataframes, each of them has the same number of rows (df1 has the same number of rows of df3, df2 of df4, and so on). The desired output should looks like this.

desired_df1 <- data.frame(colA = LETTERS[1:5], colB_1 = seq(1,5), colB_2 = seq(11,15))
desired_df2 <- data.frame(colA = LETTERS[6:10], colB_1 = seq(6,10), colB_2 = seq(16,20))
desired_list <- list(desired_df1,desired_df2)

I think I can do this with purrr::map2 but I´am not very familiar with lists and I am having difficulties with map2 formatting and indexing. So far I tried:

desired_list <- lapply(list1, 
                       function(df) {purrr::map2(.x = list1, .y = list2, .f = list1$colB_2 <- list2$colB)})

But I get:

Error in `as_mapper()`:
! Can't convert `.f`, NULL, to a function.

CodePudding user response:

Something like the following:

map2(list1, list2,
     ~data.frame(colA = .x$colA,
                 colB_1 = .x$colB,
                 colB_2 = .y$colB))

##> [[1]]
##>   colA colB_1 colB_2
##> 1    A      1     11
##> 2    B      2     12
##> 3    C      3     13
##> 4    D      4     14
##> 5    E      5     15
##> 
##> [[2]]
##>   colA colB_1 colB_2
##> 1    F      6     16
##> 2    G      7     17
##> 3    H      8     18
##> 4    I      9     19
##> 5    J     10     20




CodePudding user response:

You can use plain old Map for this:

Map(function(a, b) { a$colB_2 <- b$colB; a }, list1, list2)
#> [[1]]
#>   colA colB colB_2
#> 1    A    1     11
#> 2    B    2     12
#> 3    C    3     13
#> 4    D    4     14
#> 5    E    5     15
#> 
#> [[2]]
#>   colA colB colB_2
#> 1    F    6     16
#> 2    G    7     17
#> 3    H    8     18
#> 4    I    9     19
#> 5    J   10     20

CodePudding user response:

Or may also extract colB from list2 and transform the list1

 Map(transform, list1, colB_2 = lapply(list2, `[[`, "colB"))

-output

[[1]]
  colA colB colB_2
1    A    1     11
2    B    2     12
3    C    3     13
4    D    4     14
5    E    5     15

[[2]]
  colA colB colB_2
1    F    6     16
2    G    7     17
3    H    8     18
4    I    9     19
5    J   10     20
  • Related