I'm trying to create a function able to return various versions of the same string but with blank spaces between the letters.
something like:
input <- "word"
w ord
wo rd
wor d
We first break the string into every character using strsplit
. We then append
an empty space at every position using sapply
.
input <- "word"
input_break <- strsplit(input, "")[[1]]
c(input, sapply(seq(1,nchar(input)-1), function(x)
paste0(append(input_break, " ", x), collapse = "")))
#[1] "word" "w ord" "wo rd" "wor d"
?append
gives us append(x, values, after = length(x))
where x
is the vector, value
is the value to be inserted (here " "
) and after
is after which place you want to insert the values
.