Suppose I have a long string:
\"XOVEWVJIEWNIGOIWENVOIWEWVWEW\"
How do I split this to get every 5 characters followed by a space?
You can also use a sub-string without a loop. substring is the vectorized substr
substring
substr
x <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW" n <- seq(1, nc <- nchar(x), by = 5) paste(substring(x, n, c(n[-1]-1, nc)), collapse = " ") # [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"