问题描述
我对 mutate(across()) 函数有疑问。 在您可以在下面看到的小标题中,我想删除列中的“字母 + 下划线”(例如“p__”、“c__”等)。
A tibble: 2,477 x 4
Phylum Class Order Family
<chr> <chr> <chr> <chr>
1 " p__Proteobacteria" " c__Gammaproteobacter~ " o__Aeromonadales" " f__Aeromonadaceae"
2 " p__Bacteroidota" " c__Bacteroidia" " o__Bacteroidales" " f__Williamwhitmaniac~
3 " p__Fusobacteriota" " c__Fusobacteriia" " o__Fusobacterial~ " f__Leptotrichiaceae"
4 " p__Firmicutes" " c__Clostridia" " o__Clostridiales" " f__Clostridiaceae"
5 " p__Proteobacteria" " c__Gammaproteobacter~ " o__Enterobactera~ " f__Enterobacteriacea~
6 " p__Bacteroidota" " c__Bacteroidia" " o__Bacteroidales" " f__Williamwhitmaniac~
7 " p__Firmicutes" " c__Clostridia" " o__Lachnospirale~ " f__Lachnospiraceae"
8 " p__Bacteroidota" " c__Bacteroidia" " o__Cytophagales" " f__Spirosomaceae"
9 " p__Proteobacteria" " c__Gammaproteobacter~ " o__Burkholderial~ " f__Comamonadaceae"
10 " p__Actinobacteriot~ " c__Actinobacteria" " o__Frankiales" " f__Sporichthyaceae"
# ... with 2,467 more rows
一年前我使用了命令
table <- table %>%
mutate_at(vars(Phylum,Class,Order,Family),funs(sub(pattern = "^([a-z])(_{2})",replacement = "",.)))
现在,它提示我不再支持 funs-function 并且它不再起作用。 你对我有什么建议吗? 我想过:
taxon <- c("Phylum","Class","Order","Family")
table <- table %>%
mutate(across(taxon),gsub(pattern = "^([a-z])(_{2})",.))
但在这里我得到了错误:
Error: Invalid index: out of bounds
非常感谢:) 凯瑟琳
解决方法
你可以这样做:
library(dplyr)
taxon <- c("Phylum","Class","Order","Family")
table <- table %>% mutate(across(taxon,~gsub(pattern = "^([a-z])(_{2})",replacement = "",.)))
我没有你的数据来确认这一点,但字符串开头似乎有一个空格,应该先删除。
table <- table %>% mutate(across(taxon,trimws(.))))