I'm a newbie to R, but I'm trying to make a sliding window in R.
Using loops I can it like this, but this gets very inefficient.
results=c(1:7)
letters=c("A","B","C","D","E","F","G","H","I","J")
for(i in 1:7){
results[i]=paste(letters[i:(i+3)],collapse="")
}
How can I use an apply function to get the same output?
A little different to Ramnath's answer:
lets <- LETTERS[1:10]
substring(paste(lets,collapse=""),1:7,4:10)
#[1] "ABCD" "BCDE" "CDEF" "DEFG" "EFGH" "FGHI" "GHIJ"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With