Cool! You can also create a vector by defining its elements with the colon (:
) operator. The colon operator allows you to specify a range of numbers. For example, 1:5
generates the sequence of numbers 1, 2, 3, 4, 5, where each value in the sequence is one greater than the previous. So, instead of creating a vector this way:
id <- c(11, 12, 13, 14, 15)
You can alternatively use the following syntax:
id <- 11:15
Both lines of code will give the same exact result – numeric vector named id
with elements 11, 12, 13, 14, and 15. However, the colon operator is more convenient if you need to define vectors with many elements, such as a sequence from 1 to 100 (1, 2, 3, ..., 99, 100).
Unfortunately, with the colon operator, you're strictly limited to sequences in which each subsequent element increases by one. If you want to create a different kind of sequence, you'll need to resort to using c()
.