What are the basics of R vectors?
Vectors are an important part of R programming. They are used to store multiple elements of the same type in one variable.
What are vectors in R?
In R, vectors are a basic data structure that is used to store an ordered collection of items of the same data type. These elements can be numbers, strings, or logical values. Vectors are an important component in R because they let you organize and process data efficiently.
Unlike many other programming languages that use arrays or lists, an R vector is implemented in a special way. Vectors are homogeneous, meaning all elements in the vector must have the same data type.
Looking to take your R project online? Discover custom webspace plans from IONOS with enough space for all your projects.
What do vectors look like?
Vectors in R can be created in different ways. The simplest method is to use the c() function, which stands for “combine”.
int_vector <- c(1, 2, 3, 4, 5)
RThe vector created in this code example contains values from one to five. Note that all elements have the same data type (in this case, the “integer” data type).
Another example is creating a vector with R strings:
str_vector <- c("apple", "banana", "cherry", "pear")
RHere, the vector contains the strings “apple”, “banana”, “cherry”, and “pear”.
What do you need vectors for?
Vectors are extremely useful because they provide an efficient way to organize and process data. R vectors have many uses:
- Store data: Vectors are used to store data in a structured way. This can be useful for things like data entry.
- Filter and extract data: You can use R vectors to pick out specific parts of your data or filter based on specific criteria.
- Visualize data: Vectors are the basis for creating charts and graphs in R. This is done by plotting data from the vectors using built-in R functions.
- Perform calculations: Vectors let you apply mathematical operations quickly and efficiently to a group of data points at the same time.
Creating, deleting, and editing R vectors
Creating vectors
There are several ways to create vectors in R. In addition to the function c()
already introduced, there are other ways to create R vectors.
Create R vector with seq()
The seq()
command is usually used to create a sequence of numbers. It returns an R vector.
sequence_vector <- seq(1, 10)
RThe vector created in the code above contains numbers from 1 to 10.
Create R vector with rep()
The R command rep()
is used to repeat elements in a vector. It lets you create vectors with a repeating element easily and quickly:
repeat_vector <- rep('A', 5)
RThe R vector created here contains the letter “A” five times.
Editing vectors
There are different ways to edit vectors. For example, you can add, modify, or remove items.
Add elements
An easy way to add elements in R vectors is to use the c()
function, which adds the value passed as the second argument to the first argument:
int_vector <- c(int_vector, 6)
RChange elements
To change R vector elements, you first need to know how to access elements. To do this, you can use the index of elements, indicated in square brackets. Unless you’re just starting out in programming, then you should note that indexing in R, unlike in most other programming languages, starts at 1.
# Change the second vector element
int_vector[2] <- 100
RRemove elements
To remove elements from an R vector, you can also use the index notation. As usual, specify the index of the element you want to delete in square brackets, but prefix it with a minus sign:
# Remove the third vector element
int_vector <- int_vector [-3]
RDeleting vectors
To delete a vector in R and free up memory, you can use the rm()
function.
# Delete a vector named “int_vector”
rm(int_vector)
RMore complex vector applications
Vectors are extremely versatile and can be used in R for many purposes.
Vectorized calculations
One of R’s strengths is its ability to perform vectorized calculations. This means that you can apply mathematical operations to vectors without using loops. By applying a mathematical operation to all elements of a vector you can speed up the calculation, especially on large amounts of data.
# Vectorized computation: multiply all elements in the vector by 2
int_vector <- int_vector * 2
RIn the code example above, all elements of the R vector are multiplied by 2.
Not only can you apply mathematical R operators to all elements of a vector, you can also apply functions:
# Vectorized calculation: convert all strings to uppercase
str_vector <- toupper(str_vector)
RConditional expressions and filtering
R vectors can be used to create conditional expressions and filter data.
vector_filtered <- int_vector[int_vector > 5]
RIn the code example, all elements of the vector int_vector
that are greater than 5 are selected and stored in the vector vector_filtered
.
Statistical analyses
Vectors are also indispensable in statistics. They can be used to calculate statistical metrics like the average, median, standard deviation, and more. There are a wide range of built-in functions in R for statistical analysis on vectors.
# Calculating the average of a vector
mean_value <- mean(int_vector)
# Calculating the median of a vector
median_value <- median(int_vector)
# Calculating the standard deviation of a vector
sd_value <- sd(int_vector)
RR vectors to dataframe representations
If you want to tabulate R vectors, you can use the dataframe data structure. To combine multiple R vectors into one table, you can use the data.frame()
function.
First names <- c("Tom", "Maya")
Last names <- c("Smith", "Jones",)
# Converting vectors to a dataframe
df <- data.frame(first name = first names, last name = last names)
# Show dataframe
print(df)
RThe above code gives the following result:
First name Last name
1 Tom Smith
2 Maya Jones
RFor the conversion to a dataframe to work, the two vectors need to be the same length. The R vector length can be found with the function length()
:
int_vector <- c(2,3,4)
length(int_vector)
R