Colon

The colon operator can create multi-length vectors with steps of either +1 or -1.

x <- 1:5
print(x)
## [1] 1 2 3 4 5
print(length(x))
## [1] 5
y <- 5:1
print(y)
## [1] 5 4 3 2 1
print(length(y))
## [1] 5

Combining Sequences

We can combine sequences using the c() function.

xy <- c(1:5,5:1)
print(xy)
##  [1] 1 2 3 4 5 5 4 3 2 1
print(length(xy))
## [1] 10

class, is.numeric

We can use the colon operator to create numeric or integer sequences. We can find the type, or mode, of vector, using the class() function. It should be noted that integer sequences are numeric as well, as can be seen by the is.numeric() function. For example, is.character() tests if we have a character vector.

int.seq <- 1:5
num.seq <- 1.5:5.5
print(int.seq)
## [1] 1 2 3 4 5
print(class(int.seq))
## [1] "integer"
print(num.seq)
## [1] 1.5 2.5 3.5 4.5 5.5
print(class(num.seq))
## [1] "numeric"
print(is.numeric(int.seq))
## [1] TRUE

seq

With the seq() function we can create sequences with different steps.

x <- seq(-10,10,2)
print(x)
##  [1] -10  -8  -6  -4  -2   0   2   4   6   8  10
y <- seq(-1,1,.2)
print(y)
##  [1] -1.0 -0.8 -0.6 -0.4 -0.2  0.0  0.2  0.4  0.6  0.8  1.0