Available function to remove a character from certain line items

I am looking for a function that performs a delete operation in a row based on position.

For example, a given line such as

string1 <- "hello stackoverflow" 

Suppose I want to remove the 4th, 10th and 18th positions.

Preferred Exit

 "helo stakoverflw" 

I am not sure of the existence of such a function.

+5
source share
1 answer

It worked for me.

 string1 <- "hello stackoverflow" paste((strsplit(string1, "")[[1]])[-c(4,10,18)],collapse="") [1] "helo stakoverflw" 

I used strsplit to split a string into a character vector, and then insert only the characters you strsplit back into the string.

You can 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" 
+3
source

All Articles