Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading