Home > front end >  Combining two lists by multiplying the corresponding values in R
Combining two lists by multiplying the corresponding values in R

Time:09-23

Below is the structure of a chunk which includes two elements of list1 and list2.

list1:

list1 <- list(structure(list(chr22_20230714_G_A_b38 = 0.0000953181301665087, 
    chr22_20230737_G_A_b38 = -0.00124036704551427, chr22_20231229_T_A_b38 = 0.000808061558738542, 
    chr22_20231474_G_A_b38 = 0.000387528601423933, chr22_20231667_C_G_b38 = -0.000120624028990859), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    chr22_47157062_G_A_b38 = 0.00000909931572319958, chr22_47157212_G_A_b38 = -0.000124084106569373, 
    chr22_47157394_C_G_b38 = -0.0000752774417069946, chr22_47157559_G_A_b38 = 0.0000808446315377557, 
    chr22_47157607_T_C_b38 = 0.000237979025556899), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame")))

list2:

list2 <- list(structure(list(name = c("HG00096", "HG00097", "HG00099", 
"HG00100", "HG00101"), ENSG = c("ENSG00000040608", "ENSG00000040608", 
"ENSG00000040608", "ENSG00000040608", "ENSG00000040608"), expr = c(-0.5186894, 
0.6170779, -0.5786774, 0.07324268, -0.7579184), chr22_20230714_G_A_b38 = c(1L, 
1L, 1L, 2L, 1L), chr22_20230737_G_A_b38 = c(0L, 0L, 0L, 0L, 0L
), chr22_20231229_T_A_b38 = c(1L, 0L, 1L, 0L, 1L), chr22_20231474_G_A_b38 = c(0L, 
1L, 0L, 0L, 0L), chr22_20231667_C_G_b38 = c(1L, 1L, 1L, 2L, 1L
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(name = c("HG00096", "HG00097", "HG00099", 
"HG00100", "HG00101"), ENSG = c("ENSG00000054611", "ENSG00000054611", 
"ENSG00000054611", "ENSG00000054611", "ENSG00000054611"), expr = c(-0.5555929, 
0.1600335, 0.4027508, -0.6028474, 2.271097), chr22_47157062_G_A_b38 = c(0L, 
1L, 0L, 0L, 0L), chr22_47157212_G_A_b38 = c(0L, 0L, 1L, 1L, 2L
), chr22_47157394_C_G_b38 = c(0L, 1L, 1L, 1L, 2L), chr22_47157559_G_A_b38 = c(0L, 
1L, 0L, 0L, 0L), chr22_47157607_T_C_b38 = c(0L, 1L, 1L, 1L, 2L
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
)))

Both lists contain the same number and names of elements, as well as the same number of columns in each corresponding element. Using this assumption, I want to multiply the value of each column in list1 by the corresponding column in list2.

Desired output:

out <- list(structure(list(name = c("HG00096", "HG00097", "HG00099", 
"HG00100", "HG00101"), ENSG = c("ENSG00000040608", "ENSG00000040608", 
"ENSG00000040608", "ENSG00000040608", "ENSG00000040608"), expr = c(-0.5186894, 
0.6170779, -0.5786774, 0.07324268, -0.7579184), chr22_20230714_G_A_b38 = c(0.0000953, 
0.0000953, 0.0000953, 0.0001906, 0.0000953), chr22_20230737_G_A_b38 = c(0, 
0, 0, 0, 0), chr22_20231229_T_A_b38 = c(0.000808, 0, 0.000808, 
0, 0.000808), chr22_20231474_G_A_b38 = c(0, 0.000388, 0, 0, 0
), chr22_20231667_C_G_b38 = c(-0.000121, -0.000121, -0.000121, 
-0.000242, -0.000121)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(name = c("HG00096", "HG00097", 
"HG00099", "HG00100", "HG00101"), ENSG = c("ENSG00000054611", 
"ENSG00000054611", "ENSG00000054611", "ENSG00000054611", "ENSG00000054611"
), expr = c(-0.5555929, 0.1600335, 0.4027508, -0.6028474, 2.271097
), chr22_47157062_G_A_b38 = c(0, 0.0000091, 0, 0, 0), chr22_47157212_G_A_b38 = c(0, 
0, -0.000124, -0.000124, -0.000248), chr22_47157394_C_G_b38 = c(0, 
-0.0000753, -0.0000753, -0.0000753, -0.0001506), chr22_47157559_G_A_b38 = c(0, 
0.0000808, 0, 0, 0), chr22_47157607_T_C_b38 = c(0, 0.000238, 
0.000238, 0.000238, 0.000476)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")))

CodePudding user response:

We could use map2 or Map in base R

library(dplyr)
library(tidyr)
outnew <- map2(list2, list1,  ~ {
     dat1 <- .y
   .x %>% mutate(across(names(dat1), ~ .x * dat1[[cur_column()]] ))
   })
  •  Tags:  
  • r
  • Related