Search code examples
rseq

Don't understand the seq function


I'm reading a book on R and I don't understand the behavior of the seq function. Can someone please explain to me what it's doing when you give it a vector such as what is shown below on line 4?

> seq(1,5,1)
[1] 1 2 3 4 5
> x <- c(1,5,1)
> seq(x)
[1] 1 2 3

Solution

  • seq generates a sequence basically, so:

    seq(from, to, increment)
    

    printed out 1 to 5 incrementing by 1 each time.

    Then the c function combines lists or vectors. So it has added the variables to x and then seq is performed on x which by default calls seq_len which outputs a sequence of 1 to length(x).

    Check the documenation in the links below to see the default methods.

    Sequence generation: seq
    Combine/concatenate: c