简体   繁体   English

在 R dplyr 中,gsub() 在 mutate() 中使用列作为模式

[英]In R dplyr, gsub() in mutate() using column as the pattern

zed = data.frame(name = c('Tom', 'Joe', 'Nick', 'Bill'), names = c('TomRyanTim', 'RobJoeMike', 'SteveKevinNick', 'EvanPacJimmy'), stringsAsFactors = FALSE)
> zed
  name          names
1  Tom     TomRyanTim
2  Joe     RobJoeMike
3 Nick SteveKevinNick
4 Bill   EvanPacJimmy

> zed %>% dplyr::mutate(names = gsub(name, '', names))
  name          names
1  Tom        RyanTim
2  Joe     RobJoeMike
3 Nick SteveKevinNick
4 Bill   EvanPacJimmy

Warning message:
Problem with `mutate()` column `names`.
ℹ `names = gsub(name, "", names)`.
ℹ argument 'pattern' has length > 1 and only the first element will be used 

In the example above, the mutate(gsub()) seems to be attempting to gsub the name Tom in every row, whereas I'd like for each row to gsub() the value in the name column.在上面的示例中, mutate(gsub())似乎试图在每一行中对名称Tom进行 gsub,而我希望对每一行 gsub() 在name列中的值。 We are looking for the following output:我们正在寻找以下 output:

output$names = c('RyanTim', 'RobMike', SteveKevin', 'EvanPacJimmy')

Is it possible to update our code for the mutate + gsub to operate as such?是否可以更新我们的代码以使 mutate + gsub 正常运行?

Use rowwise : rowwise

zed %>%
  rowwise() %>% 
  mutate(names = gsub(name, '', names)) %>%
  ungroup()

To avoid using rowwise , you can use stringr::str_replace_all or stringr::str_remove_all :为避免使用rowwise ,您可以使用stringr::str_replace_allstringr::str_remove_all

library(stringr)
zed %>%
  mutate(names = str_replace_all(names, name, ""),
         names = str_remove_all(names, name))

  name  names       
  <chr> <chr>       
1 Tom   RyanTim     
2 Joe   RobMike     
3 Nick  SteveKevin  
4 Bill  EvanPacJimmy

Or group_by :group_by

library(dplyr)

zed |>
  group_by(name, names) |>
  mutate(names = gsub(name, "", names)) |>
  ungroup()

Output: Output:

# A tibble: 4 × 2
  name  names       
  <chr> <chr>       
1 Tom   RyanTim     
2 Joe   RobMike     
3 Nick  SteveKevin  
4 Bill  EvanPacJimmy

Another way is to loop through your zed data frame with sapply , and use gsub within that.另一种方法是使用sapply遍历您的zed数据框,并在其中使用gsub

library(dplyr)

zed %>%
  mutate(names = sapply(1:nrow(.), \(x) gsub(.[x, 1], "", .[x, 2])))

  name        names
1  Tom      RyanTim
2  Joe      RobMike
3 Nick   SteveKevin
4 Bill EvanPacJimmy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM