How to do sequence in R?

3

I need to do this sequence in a repeat structure to use with a bigger N, but I can not do it, does anyone have any idea how to do this?

n=8
names <- c (seq(1 , n), seq(2,n+1 ), seq (3,n+2), seq(4 ,n+3), seq(5,n+5 ),seq(6,n+5),seq(7, n+6 ), seq (8,n+7))

Thank you in advance ..

    
asked by anonymous 13.04.2015 / 00:09

2 answers

3

If what you want is a way to make the sequence more dynamic, you can use the following:

n=8
names <- rep(1:n, n)+rep(0:(n-1), each=n)
#[1]  1  2  3  4  5  6  7  8  2  3  4  5  6  7  8  9  3  4  5  6  7  8
#[23]  9 10  4  5  6  7  8  9 10 11  5  6  7  8  9 10 11 12  6  7  8  9
#[45] 10 11 12 13  7  8  9 10 11 12 13 14  8  9 10 11 12 13 14 15

In this way you create the sequence 1:n n times, and add 0, 1, 2...n-1 to each 1:n .

    
13.04.2015 / 01:13
1

Complementing with yet another answer option, you can also use lapply .

criar_seq <- function(x){
    seq(x, (n+x-1))
}

n <- 8
lapply(1:n, criar_seq)

This will return a list of n sequences with the progression of one more. If you want to put the data as a vector, just apply unlist .

    
21.06.2017 / 22:31