Vectors in R

Learn the details of the vector data type in the R language.

Vector objects

Vectors act as containers of data elements, forming one-dimensional basic data structures that transform values into a single data type. Vectors can be sorted, filtered, and modified. We can generate vectors using the c() command.

Press + to interact
An illustration of a vector object
An illustration of a vector object

Vector elements do not always keep the characteristics of their original data type if the vector includes more than one different data type. They converge in a data type that all the elements can be converted into.

The example below illustrates how the disparate data types of individual vector elements can be represented in a mutually compatible format.

Press + to interact
# Create vectors using c()
myVector <- c('data analysis', 3L, 2i+3, TRUE) # define a vector
myVector1 <- c(3L, TRUE) # define a vector
print(class(myVector)) # print the class of the variable
print(class(myVector[2])) # Check the class of the second element
print(class(myVector[3])) # Check the class of the third element
print(class(myVector1)) # Check the class of the vector

Notice how the data types are automatically converted into a common one in the examples above. The original data types of 2i+3 ...