由于特定的正则表达式,如何使用mutate(cross())更改列?
我对mutate(cross())函数有问题。 在下面的tibble中,我想删除列中的“字母+下划线”(例如“p_uuu”、“c_uu”等)由于特定的正则表达式,如何使用mutate(cross())更改列?,r,mutate,across,R,Mutate,Across,我对mutate(cross())函数有问题。 在下面的tibble中,我想删除列中的“字母+下划线”(例如“p_uuu”、“c_uu”等) A tibble: 2,477 x 4 Phylum Class Order Family <chr> <chr> <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
非常感谢:)
凯瑟琳你可以做:
library(dplyr)
taxon <- c("Phylum", "Class", "Order", "Family")
table <- table %>% mutate(across(taxon,
~gsub(pattern = "^([a-z])(_{2})", replacement = "", .)))
库(dplyr)
最后一个命令成功了,完美!谢谢:)就我的理解而言,只有一个问题,为什么我必须编写~gsub而不是单独编写gsub?这是基于公式的语法,用于跨
应用中的函数。另一种方法是使用匿名函数,将最后一部分更改为函数(x)gsub(pattern=“^([a-z])({2})”,replacement=“”,trimws(x))
taxon <- c("Phylum", "Class", "Order", "Family")
table <- table %>%
mutate(across(taxon), gsub(pattern = "^([a-z])(_{2})", replacement = "", .))
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,
~gsub(pattern = "^([a-z])(_{2})", replacement = "", trimws(.))))