Search⌘ K

Vectors in R

Explore the concept of vectors as basic one-dimensional data structures in R. Understand how to create vectors, select and filter elements, check for specific values, and replace data to maintain accuracy in data analysis tasks.

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.

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.

R
# 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 and 3L are, ...