根据元素获取匹配并订购两个列表' r中的名称

发布于 2025-01-24 05:49:49 字数 2278 浏览 0 评论 0原文

我需要匹配两个列表,list_1list_2,还可以根据元素的名称按顺序获取它们。实际上,我想删除元素,这些元素在两个列表之间并不常见。我不想考虑其嵌套列表中的任何区别。所需的输出是两个列表,包括两个列表中存在的唯一元素。 两者都有相同的结构。

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")))

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")))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-01-31 05:49:49

我们从这两个列表中获得Intersect ing name 然后使用它来子集list

nm1 <- intersect(names(list_1), names(list_2))
list_1_new <- list_1[nm1]
list_2_new <- list_2[nm1] 

We get the intersecting 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] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文