R语言 基于数据框重命名列表中的值

icomxhvb  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(92)

我使用函数“ps_venn”来比较phyloseq对象中不同样本中的分类群。此函数输出每个样本和交叉分类群的嵌套列表:

因为这是元条码数据,分类群有这些长而复杂的名称。我有一个分类谱系的数据框架,格式如下:

我想根据“类”列中的名称重命名列表中的分类群。
我没有R中的列表的经验,所以我会感谢任何指导。谢谢!
编辑:下面是我在phyloseq对象上使用的函数:

cvenn.met <- ps_venn(combined_c, group = "method", weight = FALSE, plot = FALSE)

下面是输出列表的开头:

list(OBB = c("329966c334544d14f9985b98b813f40f", 
"8e1c87829579917f8f77f7fe7a30156a"
), OES = "2f86f2cb2e0879ebd39e60982959c8bd", QBB = c("9be3f72560b678f6bbd584632672818a", 
"3a7f78f620f4733bf2344867beae26aa", "ca57149144a6a6dfdb6e14465d3e2123", 
"8612ebe6094b2f7fd25985e5c0c36226", "5a3ed6459016f5c9398eab8f051940a0", 
"c3f2f11de98c6f64740ea772e202bcbc"), QPP = "8d7b15445ca448bec893311b47510e00", 
    QPS = c("407465934116d64a8d61c12cee90b0b0", "768ed20a18290c921ed30f24e458e25a", 
    "b7a099fb2ea20e4a13fa7c52820eeb6c"), MIC__OBB__OES__OMT__QBB__QBT__QPP__QPS = c("74bda332d0a3174634f9b496b1da8d0c", 
    "cd9cac265a41b06843d41aaa1893efd5", "72e5af8afb3fdd3323fede4d49e97bda", 
    "d785682c0a83be9275e095d76cabbe36", "fd736d603728e963c8c47487a8e48755", 
    "875f4582d8e1dc661efbda0d8bb11c22", "b8615ae8b54a17ee118afe8718d7ee11"
    ))

下面是我的分类数据框架的开头:

structure(list(X = c("0021706b1ca315556a24b6d5df927e5b", "0038f2eedf8cc7893a7a9a4330aa477c", 
"003ba56d29607b45d8599085b8b69afa", "004610d70fb6092436394ca4b09bf6fb", 
"004af7f8f83f24fb7b51d8335583e14a", "0053fa60aebebf5f5e6008c70425230c"
), domain = c("Eukaryota", "Eukaryota", "Eukaryota", "Eukaryota", 
"Eukaryota", "Eukaryota"), supergroup = c("Haptista", "TSAR", 
"TSAR", "TSAR", "Obazoa", "Obazoa"), division = c("Haptophyta", 
"Alveolata", "Rhizaria", "Alveolata", "Opisthokonta", "Opisthokonta"
), subdivision = c("Haptophyta_X", NA, "Radiolaria", "Dinoflagellata", 
NA, "Metazoa"), class = c("Prymnesiophyceae", NA, "RAD-B", "Syndiniales", 
NA, "Arthropoda"), order = c("Prymnesiales", NA, "RAD-B_X", "Dino-Group-II", 
NA, "Crustacea"), family = c("Chrysochromulinaceae", NA, "RAD-B_X_Group-IVd", 
"Dino-Group-II-Clade-2", NA, "Maxillopoda"), genus = c("Chrysochromulina", 
NA, "RAD-B_X_Group-IVd_X", "Dino-Group-II-Clade-2_X", NA, NA), 
    species = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Consensus = c(0.625, 
    0.75, 0.714, 0.714, 1, 0.6)), row.names = c(NA, 6L), class = "data.frame")

我想用“class”列中相应的分类法重命名列表中的文本字符串。

rqenqsqc

rqenqsqc1#

如果您的输出名为***out***,分类法数据框***tax***,则使用lapply(出于演示目的,在修改后的数据集上),setdiff用于不匹配的字符串。

out[[3]][2] <- "003ba56d29607b45d8599085b8b69afa"
lapply(out, \(x) c(tax$class[tax$X %in% x], setdiff(x, tax$X)))
$OBB
[1] "329966c334544d14f9985b98b813f40f" "8e1c87829579917f8f77f7fe7a30156a"

$OES
[1] "2f86f2cb2e0879ebd39e60982959c8bd"

$QBB
[1] "RAD-B"                            "9be3f72560b678f6bbd584632672818a"
[3] "ca57149144a6a6dfdb6e14465d3e2123" "8612ebe6094b2f7fd25985e5c0c36226"
[5] "5a3ed6459016f5c9398eab8f051940a0" "c3f2f11de98c6f64740ea772e202bcbc"

$QPP
[1] "8d7b15445ca448bec893311b47510e00"

$QPS
[1] "407465934116d64a8d61c12cee90b0b0" "768ed20a18290c921ed30f24e458e25a"
[3] "b7a099fb2ea20e4a13fa7c52820eeb6c"

$MIC__OBB__OES__OMT__QBB__QBT__QPP__QPS
[1] "74bda332d0a3174634f9b496b1da8d0c" "cd9cac265a41b06843d41aaa1893efd5"
[3] "72e5af8afb3fdd3323fede4d49e97bda" "d785682c0a83be9275e095d76cabbe36"
[5] "fd736d603728e963c8c47487a8e48755" "875f4582d8e1dc661efbda0d8bb11c22"
[7] "b8615ae8b54a17ee118afe8718d7ee11"

相关问题