【发布时间】:2020-11-12 22:39:08
【问题描述】:
我想用s01_、s02_、s09_ 和s10_ 替换列x 的值s1_、s2_、s9_ 和s10_。我可以轻松地为每种情况(例如s1_)做到这一点,但并非对所有情况都适用(我的正则表达式知识很短)。
我怎样才能在不重复自己的情况下完成所有这些替换?
library(tidyverse)
df <- tibble( x = c('s1_', 's2_', 's9_', 's10_'))
pattern <- 's1_'
replacement <- 's01_'
stringr::str_replace(df$x, pattern, replacement)
#> [1] "s01_" "s2_" "s9_" "s10_"
Created on 2020-11-12 by the reprex package (v0.3.0)
【问题讨论】: