The most fundamental data container in R is the vector, which is simply a finite sequence of elements of the same type.
You can create a vector most simply using the c
command, followed by a comma-separated list of values. For example: c(2, 5, 7)
, c("Blue", "Yellow", "Green")
The seq
command can be used to create a regular sequence of numbers. Try the following:
You can use a colon between two integers to generate the sequence of integers from the first to the second, e.g. 17:15
will produce the sequence c(17, 16, 15)
Printed vectors appear often in multiple lines, where the start of a line indicates in brackets the index for the first element in that line, starting from 1.
You can find out the length of a vector using length
:
Operations on vectors happen element-wise: