Igor Igor - 5 months ago 45
R Question

Insert blank space between letters of word

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download