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
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"
来源:https://stackoverflow.com/questions/31761627/how-to-replace-nth-character-of-a-string-in-a-column-in-r