0

I am new to R and am wondering what is the difference between seq() and sequence(). Both entered into R give different results:

> seq(c(10,5))
[1] 1 2
> sequence(c(10,5))
 [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5

Can anyone help me with this?

maxshuty
  • 9,708
  • 13
  • 64
  • 77
rocc
  • 153
  • 1
  • 7

1 Answers1

6

A good place to start if you have questions is to take a look at the help files using the help() or ? commands.

If you look at the help file from ?seq, it will say that it usually takes some arguments:

## Default S3 method:
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)

So if you do something like seq(from = 1, to = 10) or seq(1, 10), it will give you a vector from 1 to 10:

seq(1, 10)
[1] 1  2  3  4  5  6  7  8  9  10

Your usage of seq() is not seen much, but the behavior is documented lower down in the help file

[seq(from)] generates the sequence 1, 2, ..., length(from) (as if argument along.with had been specified), unless the argument is numeric of length 1 [...]

Therefore, seq(c(1, 10)) will output a sequence of "1, 2" because the only argument it was given was a vector of length 2.

The behavior of sequence() is more straight-forward, and is explained succinctly in the help file (accessed from ?sequence). Also, an example of the behavior is shown at the bottom of the help file.

sequence(c(3, 2)) # the concatenated sequences 1:3 and 1:2.
#> [1] 1 2 3 1 2

Welcome to the wonderful, yet quirky, world of programming in R. I suggest you learn the basics of the R language first, and look to the help files and documentation before asking basic questions, reserving stackoverflow as a resource for resolving more difficult programming questions.

ialm
  • 8,510
  • 4
  • 36
  • 48