我有几个具有丰富的特定特性(OTU ID)的数据帧。例如,table1:
A tibble: 6 x 46
`OTU ID` V1.1.K1 V1.1.K2 V1.1.K3 V1.1.N1 V1.1.N2 V1.14.K16 V1.14.K17 V1.14.K18 V1.14.N0 V1.14.W16 V1.14.W17
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 107273e~ 2239 2861 1404 10255 585 4448 8340 5027 934 241 4983
2 bf83e8b~ 174 95 406 4054 408 23383 6820 31184 2046 411 645
3 b744eae~ 49 35 126 3440 245 83 443 18 11205 121 388
4 0e40fa4~ 86 38 165 1015 111 10022 9973 228 90 638 2671
5 33ee4bb~ 50 50 53 68 34 665 278 4816 218 38 136
6 c1c17b3~ 2366 7838 7827 471 60 160 27 37 190 12 303
# ... with 34 more variables: V1.14.W18 <dbl>, V1.2.K4 <dbl>, V1.2.K5 <dbl>, V1.2.K6 <dbl>, V1.2.W4 <dbl>,
# V1.2.W5 <dbl>, V1.2.W6 <dbl>, V1.21.K19 <dbl>, V1.21.K20 <dbl>, V1.21.K21 <dbl>, V1.21.N0 <dbl>,
# V1.21.W19 <dbl>, V1.21.W20 <dbl>, V1.21.W21 <dbl>, V1.3.K7 <dbl>, V1.3.K8 <dbl>, V1.3.K9 <dbl>,
# V1.3.W7 <dbl>, V1.3.W8 <dbl>, V1.3.W9 <dbl>, V1.4.K10 <dbl>, V1.4.K11 <dbl>, V1.4.K12 <dbl>, V1.4.W10 <dbl>,
# V1.4.W11 <dbl>, V1.4.W12 <dbl>, V1.7.K13 <dbl>, V1.7.K14 <dbl>, V1.7.K15 <dbl>, V1.7.N0 <dbl>,
# V1.7.W13 <dbl>, V1.7.W14 <dbl>, V1.7.W15 <dbl>, sum <dbl>
其他表看起来相似,只是丰度不同。然后我有一个分类法数据框架,其中包含有关特性的信息(OTU ID)。它被称为分类法,看起来像这样:
`OTU ID` Taxon Confidence
<chr> <chr> <dbl>
1 00009522ba8d1badfe92207~ d__Bacteria; p__Desulfobacterota; c__Desulfovibrionia; o__Desulfovibrional~ 0.966
2 0000fb10633e8ad10a0f0b7~ d__Bacteria; p__Actinobacteriota; c__Actinobacteria; o__Micrococcales; f__~ 0.999
3 0001d123420b59585627edf~ d__Bacteria; p__Proteobacteria; c__Alphaproteobacteria; o__Rhizobiales; f_~ 1.00
4 0001f7b8d2a9d04fbd0df3e~ d__Bacteria; p__Bacteroidota; c__Bacteroidia; o__Cytophagales; f__Bernarde~ 1.00
5 00026915bdc887a8587e62e~ d__Bacteria; p__Planctomycetota; c__Pla4_lineage; o__Pla4_lineage; f__Pla4~ 0.997
6 0004a49903f29d739411b1c~ d__Archaea; p__Nanoarchaeota; c__Nanoarchaeia; o__Woesearchaeales; f__GW20~ 0.777
由于OTU ID -列,丰度表应该与分类法表合并。对于一个数据帧,它使用以下代码:
table1_tax <- left_join(table1, taxonomy, by = "OTU ID")
现在我想把命令包含在for循环中,循环遍历我所有的丰度表,每个变量都应该与分类法文件结合起来,并分配给我可以使用的新变量。我不擅长for循环,所以请原谅我的错误。
#' get the tables in a list
tables <- mget(ls()[1:9])
#' as left_join can not loop over a list, I just extracted the names of the abundance tables in the variable. Was this step correct?
tables <- names(tables)
#' For Loop
for(i in 1:length(tables)) {
assign(paste0("tax_", tables[i]), left_join(tables[i], taxonomy, by= setnames("OTU ID")))
}
我得到的错误是:
Error in UseMethod("left_join") :
no applicable method for 'left_join' applied to an object of class "character"
我该如何解决这个问题?你知道吗?谢谢你:)凯瑟琳
您是否得到了您想要的?
library(tidyverse)
# fake tables
df1 <- tribble(
~OTUID, ~V1, ~V2, ~V3, ~V4,
"09522ba", 2239, 2861, 1404, 10255,
"0fb1063", 174, 95, 406, 4054,
"b744eae", 49, 35, 126, 3440
)
df2 <- df3 <- df4 <- df1
# named list of tables
lst_tables <- list(df1 = df1, df2 = df2, df3 = df3, df4 = df4)
# fake taxonomy
taxo <-
tribble(
~OTUID, ~Taxon, ~Confidence,
"09522ba", "d__Bacteria p__Desulfobacterota c__Desulfovibrionia", 0.966,
"0fb1063", "d__Bacteria p__Actinobacteriota c__Actinobacteria", 0.999,
"1d12342", "d__Bacteria p__Proteobacteria c__Alphaproteobacteria", 1.00,
)
# 1) if you really want the loop
outbis <- list()
for(i in seq_along(lst_tables)) {
outbis[[i]] <- left_join(lst_tables[[i]], taxo, by = "OTUID")
names(outbis)[i] <- paste0("tax_", names(lst_tables)[i])
}
# 2) without the loop
out <-
map(lst_tables, ~ left_join(.x, taxo))