Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top