Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according to the precedence. There are various operations that can be performed on vectors in R.
Creating a vector
Vectors can be created in many ways as shown in the following example. The most usual is the use of ‘c’ function to combine different elements together.
using c function 1 4 5 2 6 7 using seq function 1.00 3.25 5.50 7.75 10.00 using colon 5 6 7 8 9 10
Accessing vector elements
Vector elements can be accessed in many ways. The most basic is using the ‘’, subscript operator. Following are the ways of accessing Vector elements:
Note: vectors in R are 1 based indexed, unlike the normal C, python, etc format where indexing starts from 0.
using Subscript operator 5 using c function 1 4 Logical indexing 5 4 4
Modifying a vector
Vectors can be modified using different indexing variations which are mentioned in the below code:
Using subscript operator 2 5 11 7 8 2 Logical indexing 2 5 0 7 8 2 using c function 8 5 2
Deleting a vector
Vectors can be deleted by reassigning them as NULL. To delete a vector we use the NULL operator.
Deleted vector NULL
We can perform arithmetic operations between 2 vectors. These operations are performed element-wise and hence the length of both the vectors should be the same.
Addition 12 11 6 6 53 3 Subtracttion -2 -7 4 -4 49 1 Multiplication 35 18 5 5 102 2 Division 0.7142857 0.2222222 5.0000000 0.2000000 25.5000000 2.0000000
Sorting of Vectors
For sorting we use the
sort() function which sorts the vector in ascending order by default.
sorting done in ascending order 1 2 2 5 5 51 sorting done in descending order 51 5 5 2 2 1
- List of Vectors in R
- Types of Vectors in R Programming
- Create Matrix from Vectors in R
- Assigning Vectors in R Programming
- Append Operation on Vectors in R Programming
- Compute the Covariance between Two Vectors in R Programming - cov() Function
- Creating a Data Frame from Vectors in R Programming
- Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
- Generating sequenced Vectors in R Programming - sequence() Function
- Find positions of Matching Elements between Vectors in R Programming - match() Function
- Generate Color Vectors of desired Length in R Programming - rainbow() Function
- Get the natural logarithm of the beta value of Non-Negative numeric vectors in R Language - lbeta() Function
- Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Create a Data Frame of all the Combinations of Vectors passed as Argument in R Programming - expand.grid() Function
- Compute the beta value of Non-Negative Numeric Vectors in R Programming - beta() Function
- Getting and Setting Length of the Vectors in R Programming - length() Function
- Operations on Matrices in R
- DataFrame Operations in R
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.