在 R 中替换那些没有 leading/ending 空格的字符串

Replace those string without the leading/ending whitespace in R

我正在尝试替换那些没有前导或结尾空格的字符串。但问题是 str_detect() 也会影响组中的那些字符串。有没有一种方法可以替换那些没有 leading/ending 空格的字符串?

# A tibble: 4 x 2
  rowid brand     
  <int> <chr>     
1     1 apple     
2     2 Apple     
3     3 apple cafe
4     4 Apple cafe

输出:

structure(list(rowid = 1:4, brand = c("apple", "Apple", 
"apple cafe", "Apple cafe")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

这是我的工作脚本:

df %>%
  mutate(brand = case_when(
    str_detect(brand, "apple") ~ "Orange",
    TRUE ~ brand)) %>%   
  group_by(brand) %>% 
  tally()

预期产出

# A tibble: 4 x 2
  rowid brand     
  <int> <chr>     
1     1 Orange     
2     2 Orange     
3     3 Apple cafe
4     4 Apple cafe

如果你想匹配整个单词不需要使用正则表达式:

library(dplyr)

df %>%
  mutate(brand = case_when(tolower(brand) == 'apple' ~ "Orange",
                            TRUE ~ brand))

#  rowid brand     
#  <int> <chr>     
#1     1 Orange    
#2     2 Orange    
#3     3 apple cafe
#4     4 Apple cafe

或以 R 为基数:

df$brand[tolower(df$brand) == 'apple'] <- 'Orange'