Vector Sequences

In R there a few different ways to generate vector sequences. One way to do this is to use an interative loop. The easiest way is to use the colon operator. It produces a vector consisting a range of numbers.

 [1]  1  2  3  4  5  6  7  8  9 10

The Sequence function generates a sequence in arithmetric progression.

 [1]  1  2  3  4  5  6  7  8  9 10
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
[1] "201810" "201830" "201850" "201870"
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
 [1] 0.00000000 0.05263158 0.10526316 0.15789474 0.21052632 0.26315789
 [7] 0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.57894737
[13] 0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.89473684
[19] 0.94736842 1.00000000



Repeating Vector Sequence

  • The rep() function will replicate the same values. It is a generic function that can be used with any data structure. The times argument will specify the number of times to be repeated and the length of the vector will be times*length(x).
  • The optional each argument will specify the number of times each element in the vector to repeated.
 [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10
 [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
[1] "Fall 2014 FTFY" "Fall 2015 FTFY" "Fall 2016 FTFY" "Fall 2017 FTFY"
[5] "Fall 2018 FTFY"
 [1] "201410" "201430" "201450" "201470" "201510" "201530" "201550"
 [8] "201570" "201610" "201630" "201650" "201670" "201710" "201730"
[15] "201750" "201770" "201810" "201830" "201850" "201870"



Elements of a Vector

Unique and Duplicated Elements

  • unique(): A generic function that returns only the unique elements of a vector
  • duplicated(): determines which elements are duplicated
 [1]  9 10 11 12 13 14 15 16 17 18 19 20  1  2  3  4  5  3  4  5  6  7  0
[24]  1  2  3  4  5  6  7  8
 [1]  9 10 11 12 13 14 15 16 17 18 19 20  1  2  3  4  5  6  7  0  8
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[12] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
[23] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
[1] 3 4 5 1 2 6 7

Boolean Tests

  • any(..., na.rm = TRUE) will test whether a condition is true for at least one of the elements in a vector.
  • all(..., na.rm = TRUE) will test whether a condition is true for all of the elements in a vector.
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE