简体   繁体   中英

preprocessing in R

I have a string for example string A="however". Now my I have a variable alpha whose value keeps changing like alpha="er","ho","er",etc.I want to remove this alpha value from the string

when the value of alpha is "er" the final value should be "however"-"er"="howev" and when the value of alpha is "ho" the final value should be "however"-"ho"="wever" and so on.How can I extract this final string.

Thanks

A <- "however" 
alpha <- c("er","ho")
sapply(alpha, gsub, "", A)
#     er      ho    
# "howev" "wever" 

Actually found its very easy

gsub(alpha,"",A) 

would do it

You may use package stringr . Function str_replace will do the trick.

library(stringr)
A<-c("however", "evermore")
alpha <- "er"
str_replace(string=A, pattern=alpha, replacement="")

You may also substitute the substring you are removing by anything you put in replacement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM