首页 > 解决方案 > 仅当字符串位于 R 中字符串的开头时,才从多列中删除字符串

问题描述

我有一个数据框,其中包含许多名为 fact 的列(在此示例中为 fact1、fact2 和 fact3),其中包含字符。它们都以 old_ 开头,我想删除它。我的真实数据集有很多列,所以我不想对每一列都做。我在这里看到了一个解决方案Getting and remove the first character of a string,但是当我尝试应用它时,我得到一个错误。

library(tidyverse)
problem <- tibble(name = c("Random", "Silly"), height = c(48, 50), weight = c(95, 102), fact1 = c("old_song_yes", "old_dance_no"), fact2 = c("old_bold_yes", "old_shy_no"), fact3 = c("old_cold_yes", "old_young_yes"))

这是我的解决方案,但它的错误消息不起作用:

apply(problem, substring(problem, 5, nchar(problem)))
Error in match.fun(FUN) : argument "FUN" is missing, with no default

重要的是它只从一开始就删除字符串;否则,例如,在 fact3 列中,它将看起来像“cyes”而不是“cold_yes”。

最后,以防万一,我在此处包含了我希望最终产品的外观:

library(tidyverse)
solution <- tibble(name = c("Random", "Silly"), height = c(48, 50), weight = c(95, 102), fact1 = c("song_yes", "dance_no"), fact2 = c("bold_yes", "shy_no"), fact3 = c("cold_yes", "young_yes"))

如果您有一个整洁的解决方案或仅涉及事实列的解决方案,我将特别感激,谢谢!

标签: rstringsubstringstringrgrepl

解决方案


一种dplyr可能是:

problem %>%
 mutate_at(vars(starts_with("fact")), list(~ sub("^old_", "\\1", .)))

  name   height weight fact1    fact2    fact3    
  <chr>   <dbl>  <dbl> <chr>    <chr>    <chr>    
1 Random     48     95 song_yes bold_yes cold_yes 
2 Silly      50    102 dance_no shy_no   young_yes

或者:

problem %>%
 mutate_at(vars(starts_with("fact")), list(~ substr(., 5, nchar(.))))

推荐阅读