I need to get matched the two lists, list_1
and list_2
and also get them in order based on elements' name. In fact, I want to remove the elements, those are not common between two lists. I do not want to consider any difference in their nested lists. The desired outputs are two lists including the only elements whose exist in both lists.
Both have same structure.
list_1 <- list(ENSG0000014 = structure(list(name = c("E-1122O", "E-11EM3",
"E-11EMC", "E-1442O", "E-1132O"), ENSG = c("ENSG0000014", "ENSG0000014",
"ENSG0000014", "ENSG0000014", "ENSG0000014"), expr = c(" 9.940670e-02",
" 1.289670e-01", "-7.394904e-03", " 9.940670e-02", " 9.940670e-02"
), `1_43222779_A_G_b37` = c("1", "1", "2", "1", "0"), `1_43222856_A_G_b37` = c("0",
"0", "0", "1", "1"), `1_43223126_C_T_b37` = c("0", "1", "0",
"1", "2"), `1_43223317_T_C_b37` = c("1", "0", "0", "2", "1")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame")), ENSG0000015 = structure(list(
name = c("E-1122O", "E-11EM3", "E-11EMC", "E-1442O", "E-1132O"
), ENSG = c("ENSG0000015", "ENSG0000015", "ENSG0000015",
"ENSG0000015", "ENSG0000015"), expr = c(" 9.940670e-02",
" 1.289670e-01", "-7.394904e-03", " 9.940670e-02", " 1.289670e-01"
), `1_43222779_A_G_b37` = c("0", "1", "0", "1", "2"), `1_43222856_A_G_b37` = c("1",
"1", "2", "1", "0")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")))
list_2 <- list(ENSG0000014 = structure(list(name = c("E-1122O", "E-11EM3",
"E-11EMC", "E-1442O", "E-1132O"), ENSG = c("ENSG0000014", "ENSG0000014",
"ENSG0000014", "ENSG0000014", "ENSG0000014"), expr = c(" 9.940670e-02",
" 1.289670e-01", "-7.394904e-03", " 9.940670e-02", " 9.940670e-02"
), `1_43222779_A_G_b37` = c("1", "1", "2", "1", "0"), `1_43222856_A_G_b37` = c("0",
"0", "0", "1", "1"), `1_43223126_C_T_b37` = c("0", "1", "0",
"1", "2"), `1_43223317_T_C_b37` = c("1", "0", "0", "2", "1")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame")), ENSG0000016 = structure(list(
name = c("E-1122O", "E-11EM3", "E-11EMC", "E-1442O", "E-1132O"
), ENSG = c("ENSG0000015", "ENSG0000015", "ENSG0000015",
"ENSG0000015", "ENSG0000015"), expr = c(" 9.940670e-02",
" 1.289670e-01", "-7.394904e-03", " 9.940670e-02", " 1.289670e-01"
), `1_43222779_A_G_b37` = c("0", "1", "0", "1", "2"), `1_43222856_A_G_b37` = c("1",
"1", "2", "1", "0")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame")))
CodePudding user response:
We get the intersect
ing names from both lists and then use that to subset the list
nm1 <- intersect(names(list_1), names(list_2))
list_1_new <- list_1[nm1]
list_2_new <- list_2[nm1]