Updated March 21, 2023
Introduction to R Vectors
R vectors are the simplest form of data structure that holds elements of primitive data types. R language supports a built-in function c() to create the vector-type variables and assign values to them. Also, there is ‘ : ‘ the operator is used to create and define the vector variable. To access the elements from the vector variable, the R language uses the index position, which starts with index 1 for the first element of the vector variable. These are highly used by the R programmer for storing and processing data. R language supports modifying and removing an element from the vector variables.
Single Element Vectors
The data created with one value is of the vector of length 1.
- character
Example: print(“xyz”)
- Integer
Example: print(54L)
- Double
Example: print(6.5)
- Complex
Example: print(45L)
- logical
Example: print(FALSE)
- Raw
Example: print(1+3i)
Multiple Element Vector
We can also create a vector with multiple values using the colon with numerical data.
Example:
v <- 5:10
print(v)
Output: 5 6 7 8 9 10
The sequence of decimal values.
v <- 6.6:9.6
print(v)
Output: 6.6 7.6 8.6 9.6
In the below example,11.4 will not be in the sequence, and it will be discarded as it doesn’t belong in the sequence.
v <- 3.8:8.4
print(v)
Output: 3.8 4.8 5.8 6.8 7.8
Recommended Articles
This is a guide to R Vectors. Here we discuss the different types of vector functions with syntax and examples respectively. You may also have a look at the following articles to learn more –