March 2018
Beginner to intermediate
570 pages
13h 42m
English
It is very common to want to extract one or more elements from a vector. For this, we use a technique called indexing or subsetting. After the vector, we put an integer in square brackets ([]) called the subscript operator. This instructs R to return the element at that index. The indices (plural for index, in case you were wondering!) for vectors in R start at 1 and stop at the length of the vector:
> our.vect[1] # to get the first value [1] 8 > # the function length() returns the length of a vector > length(our.vect) [1] 7 > our.vect[length(our.vect)] # get the last element of a vector [1] 9
Note that in the preceding code, we used a function in the subscript operator. In cases like these, R evaluates the expression in the ...