Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use paste to combine letters instead or loops. R

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?

like image 974
user3246796 Avatar asked Feb 24 '14 04:02

user3246796


1 Answers

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"
like image 161
thelatemail Avatar answered Sep 23 '22 23:09

thelatemail