Remove nth whitespace from string

This function splits the string based on " ", pastes together the n and n+1'th elements of the split string and then pastes together the rest of the string without the n+1 elements of the split string.

remove_n_ws = function(n, my_string) {  
    sub_str = unlist(strsplit(my_string, " "))
    sub_str[n] = paste(sub_str[n], sub_str[n+1], sep="")
    paste(sub_str[-c(n+1)], collapse=" ")
}
> remove_n_ws(1, my_string)
[1] "thisis a string"
> remove_n_ws(2, my_string)
[1] "this isa string"
> remove_n_ws(3, my_string)
[1] "this is astring"

Here is an option with str_locate/str_sub from stringr - get the start/end position index of all spaces with str_locate_all, then use str_sub to assign those locations to blank ("") and retrun the string

library(stringr)
remove_n_ws <- function(str1, n = 1) {
   
       loc <- str_locate_all(str1, "\\s+")[[1]][n,]
       str_sub(str1, loc[1], loc[2]) <- ""
       str1
}

-testing

> remove_n_ws(my_string, 1)
[1] "thisis a string"
> remove_n_ws(my_string, 2)
[1] "this isa string"
> remove_n_ws(my_string, 3)
[1] "this is astring"

1) Base R Generate the regular expression using sprintf and then run it with sub. No packages are used.

# input
n <- 2
string <- "this is a string"

s <- sprintf("((\\s+\\S+){%d})\\s+", n-1)
sub(s, "\\1", string)
## [1] "this isa string"

In the special case that n = 1 the above still works but we could alternately use just:

sub("\\s+", "", string)
## [1] "thisis a string"

2) gsubfn Using the same input we can use gsubfn. Define a proto object whose fun method returns "" if the count is n and returns its argument, the matched string, otherwise. This can be easily generalized. For example if we wanted to remove the 2nd, 3rd and 4th instance then if could test count %in% 2:4 instead.

library(gsubfn)

p <- proto(fun = function(., x) if (count == n) "" else x)
gsubfn("\\s+", p, string)
## [1] "this isa string"