# Input
n <- 2
"abcd"
# Output
c("ab", "bc", "cd")
I don't want to use a for loop or sapply
On
This embed trick could work but might be not as efficient as the substring approach by @Ronak Shah
> n <- 2
> s <- "abcd"
> apply(embed(utf8ToInt(s), n)[, n:1], 1, intToUtf8)
[1] "ab" "bc" "cd"
You may use
substring-