how to replace nth character of a string in a column in r

人盡茶涼 提交于 2019-12-01 22:24:09

You may use sub,

sub("^([^_]*_[^_]*)_", "\\1-",df$a)

Example:

> a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh")
> b<-c("a","b")
> df<-data.frame(cbind(a,b))
> df
              a b
1 aa_bbb_cc_ddd a
2 ee_fff_gg_hhh b
> df$a <- sub("^([^_]*_[^_]*)_", "\\1-",df$a)
> df
              a b
1 aa_bbb-cc_ddd a
2 ee_fff-gg_hhh b

If things are as consistent as you show and you want to replace the 7th character then substring may be a good way to go, but you made the column character by wrapping with data.frame without stringsAsFactors = FALSE. You'd need to make the column character first:

df$a <- as.character(df$a)
substring(df$a, 7, 7) <- "-"
df

##               a b
## 1 aa_bbb-cc_ddd a
## 2 ee_fff-gg_hhh b

Here's a general way to replace the nth occurrence of _ with -.

n <- 2

# create regex pattern based on n
pat <- paste0("^((?:.*?_){", n - 1, "}.*?)_")
# [1] "^((?:.*?_){1}.*?)_"

# replace character
sub("^((?:.*?_){1}.*?)_", "\\1-", df$a, perl = TRUE)
# [1] "aa_bbb-cc_ddd" "ee_fff-gg_hhh"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!