Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于特定的正则表达式,如何使用mutate(cross())更改列?_R_Mutate_Across - Fatal编程技术网

由于特定的正则表达式,如何使用mutate(cross())更改列?

由于特定的正则表达式,如何使用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

我对mutate(cross())函数有问题。 在下面的tibble中,我想删除列中的“字母+下划线”(例如“p_uuu”、“c_uu”等)

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(.))))