You
can access elements of vector using indexes.
Prevoius
Next
Home> vector1 <- c("h", "e", "l", "l", "o", "vector") > vector1[1] [1] "h" > vector1[2] [1] "e" > vector1[3] [1] "l" > vector1[4] [1] "l" > vector1[5] [1] "o" > vector1[6] [1] "vector"
Unlike other
languages, indexes in R start from 1.
Negative indexes
If the index
is negative, then it return all the members, other than one member whose
position has the same absolute value as the negative index.
> vector1 [1] "h" "e" "l" "l" "o" "vector" > vector1[-1] [1] "e" "l" "l" "o" "vector" > vector1[-2] [1] "h" "l" "l" "o" "vector" > vector1[-3] [1] "h" "e" "l" "o" "vector" > vector1[-4] [1] "h" "e" "l" "o" "vector" > vector1[-5] [1] "h" "e" "l" "l" "vector" > vector1[-6] [1] "h" "e" "l" "l" "o"
Range index
You can
retrieve the elements of vector, from start index to stop index.
Syntax
vectorName[start:stop]
Returns
elements from start to stop (inclusive).
> vector1 [1] "h" "e" "l" "l" "o" "vector" > vector1[2:4] [1] "e" "l" "l" > vector1[1 : length(vector1)] [1] "h" "e" "l" "l" "o" "vector"
You can get
a new vector, by using the specific indexes from given vector.
> vector1[1 : length(vector1)] [1] "h" "e" "l" "l" "o" "vector" > > vector1[c(2, 5)] [1] "e" "o" > vector1[c(2, 1,5,6)] [1] "e" "h" "o" "vector"
No comments:
Post a Comment