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
Use str_replace() to Replace Character in a String packages("stringr") . It is used to replace a part of a string (character) on a column with another string or a character.
Use str_replace() method from stringr package to replace part of a column string with another string in R DataFrame.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With