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

此生再无相见时 提交于 2019-12-02 00:57:14

问题


My input is

a<-c("aa_bbb_cc_ddd","ee_fff_gg_hhh")
b<-c("a","b")
df<-data.frame(cbind(a,b))

I want my output to be

a<-c("aa_bbb-cc_ddd","ee_fff-gg_hhh")
b<-c("a","b")
df<-data.frame(cbind(a,b))

please help


回答1:


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



回答2:


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



回答3:


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"


来源:https://stackoverflow.com/questions/31761627/how-to-replace-nth-character-of-a-string-in-a-column-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!