I am looking for a function which performs delete operation on a string based on the position.
For example, given string is like that
string1 <- "hello stackoverflow"
Suppose, I want to delete 4th,10th and 18th positions.
Preferred Output
"helo stakoverflw"
I am not sure about the existence of such function.
This worked for me.
string1 <- "hello stackoverflow"
paste((strsplit(string1, "")[[1]])[-c(4,10,18)],collapse="")
[1] "helo stakoverflw"
I used strsplit
to split the string into a vector of characters, and then pasted only the desired characters back together into a string.
You could also write a function that does this:
delChar <- function(x,eliminate){
paste((strsplit(x,"")[[1]])[-eliminate],collapse = "")
}
delChar(string1,c(4,10,18))
[1] "helo stakoverflw"
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