根据数据框重命名列表中的值

huangapple go评论66阅读模式
英文:

Rename values in a list based on a dataframe

问题

我使用函数“ps_venn”来比较不同样本中存在的分类单元,使用了一个phyloseq对象。这个函数输出了每个样本和相交分类单元的嵌套列表:

因为这是代谢条形码数据,分类单元具有复杂的长名称。我有一个按如下格式格式化的分类单元系谱的数据框:

我想根据“class”列中的名称重命名列表中的分类单元。

我在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”列中对应的分类单元。

英文:

I am using the function "ps_venn" to compare taxa present in different samples with a phyloseq object. This function outputs a nested list of each sample(s) and the intersecting taxa:

根据数据框重命名列表中的值

Because this is metabarcoding data, the taxa have these long complicated names. I have a dataframe of the taxa lineages formatted like this:
根据数据框重命名列表中的值

I would like to rename the taxa in the list based on the name in the "class" column.

I have no experience with lists in R, so I'd appreciate any guidance. Thanks!

Edit:
Here is the function I used on my phyloseq object:

cvenn.met &lt;- ps_venn(combined_c, group = &quot;method&quot;, weight = FALSE, plot = FALSE)

Here is the beginning of the output list:

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

Here is the beginning of my taxonomy dataframe:

structure(list(X = c(&quot;0021706b1ca315556a24b6d5df927e5b&quot;, &quot;0038f2eedf8cc7893a7a9a4330aa477c&quot;, 
&quot;003ba56d29607b45d8599085b8b69afa&quot;, &quot;004610d70fb6092436394ca4b09bf6fb&quot;, 
&quot;004af7f8f83f24fb7b51d8335583e14a&quot;, &quot;0053fa60aebebf5f5e6008c70425230c&quot;
), domain = c(&quot;Eukaryota&quot;, &quot;Eukaryota&quot;, &quot;Eukaryota&quot;, &quot;Eukaryota&quot;, 
&quot;Eukaryota&quot;, &quot;Eukaryota&quot;), supergroup = c(&quot;Haptista&quot;, &quot;TSAR&quot;, 
&quot;TSAR&quot;, &quot;TSAR&quot;, &quot;Obazoa&quot;, &quot;Obazoa&quot;), division = c(&quot;Haptophyta&quot;, 
&quot;Alveolata&quot;, &quot;Rhizaria&quot;, &quot;Alveolata&quot;, &quot;Opisthokonta&quot;, &quot;Opisthokonta&quot;
), subdivision = c(&quot;Haptophyta_X&quot;, NA, &quot;Radiolaria&quot;, &quot;Dinoflagellata&quot;, 
NA, &quot;Metazoa&quot;), class = c(&quot;Prymnesiophyceae&quot;, NA, &quot;RAD-B&quot;, &quot;Syndiniales&quot;, 
NA, &quot;Arthropoda&quot;), order = c(&quot;Prymnesiales&quot;, NA, &quot;RAD-B_X&quot;, &quot;Dino-Group-II&quot;, 
NA, &quot;Crustacea&quot;), family = c(&quot;Chrysochromulinaceae&quot;, NA, &quot;RAD-B_X_Group-IVd&quot;, 
&quot;Dino-Group-II-Clade-2&quot;, NA, &quot;Maxillopoda&quot;), genus = c(&quot;Chrysochromulina&quot;, 
NA, &quot;RAD-B_X_Group-IVd_X&quot;, &quot;Dino-Group-II-Clade-2_X&quot;, 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 = &quot;data.frame&quot;)

I would like to rename the strings of text in my list with their corresponding taxonomy in the "class" column.

答案1

得分: 0

如果您的输出被称为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"
英文:

If your output is called out and the taxonomy data frame tax, using lapply (on a modified dataset for demonstration purposes), with setdiff for the non-matching strings.

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

$OES
[1] &quot;2f86f2cb2e0879ebd39e60982959c8bd&quot;

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

$QPP
[1] &quot;8d7b15445ca448bec893311b47510e00&quot;

$QPS
[1] &quot;407465934116d64a8d61c12cee90b0b0&quot; &quot;768ed20a18290c921ed30f24e458e25a&quot;
[3] &quot;b7a099fb2ea20e4a13fa7c52820eeb6c&quot;

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

huangapple
  • 本文由 发表于 2023年6月16日 00:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定