Accessing R vector elements
Accessing elements in a vector is the process of performing operation on an individual element of a vector. There are many ways through which we can access the elements of the vector. The most common is using the ‘[]’, symbol.
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.
R
# R program to access elements of a Vector # accessing elements with an index number. X<- c (2, 5, 18, 1, 12) cat ( 'Using Subscript operator' , X[2], '\n' ) # by passing a range of values # inside the vector index. Y<- c (4, 8, 2, 1, 17) cat ( 'Using combine() function' , Y[ c (4, 1)], '\n' ) |
Output:
Using Subscript operator 5
Using combine() function 1 4
R Vectors
R Vectors are the same as the arrays in R language which are used to hold multiple data values of the same type. One major key point is that in R Programming Language the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.
Contact Us