Create Vectors In R: A Step-by-step Tutorial

by | R

This tutorial will show you how to use and create vectors in R. Vectors allow you to work with multiple pieces of data and then assign them into a single object.

A vector in R looks a lot like a range in Excel. However, unlike in Excel, a vector’s elements should all be of the same type.

create vectors in R

In the image above, one line represents one vector. You can see that in each vector, all its elements are of the same type. The first line is numerical, followed by string, and then logical.

This tutorial will focus on two things: combining data and manipulating vectors.

Create Vectors In R By Combining Data

Combining data in R is easy. All you need to do is use the c ( ) function.

So, open your RStudio. Assign multiple values to object a using the c ( ) function, similar to what is shown below.

create vectors in R

If you print this, you’ll see that the value of a is 1, 2, and 3.

Now remember that vectors should only contain elements of the same type. So, what happens if you mix two different object classes together? Here’s an example:

create vectors in R

If you assign two numerical values to b and then assign a string as the third one, you’ll see that all the values in b are converted to a character.

This is called coercion. It’s where R decides the best way to convert the elements into the same object class. In this case, the best way was to convert the numbers to text instead of the other way around.

Analyze and Manipulate Vectors In R

You can also perform mathematical operations on vectors.

For example, if you multiply a by 2, you’ll see that each numerical element in a was multiplied by 2.

This is similar to multiplying a range in Excel or multiplying a column in Power BI.

Now let’s try out another case.

Let’s create a new object called my_long_vector and assign a range from 5 to 84. So instead of using the c ( ) function, you can use a colon (:) to indicate a range of values.

create vectors in R

When you print this, you’ll see that the object my_long_vector contains all the values from 5 to 84.

You can also locate a specific element in a vector. This is called indexing. You can do this by following the object name with square brackets ( [ ] ) and then placing in the position of the element you want.

For example, you want to find the 3rd element for my_long_vector. All you need to do is execute my_long_vector [3]. You’ll then arrive with 7 as the answer.

Regardless of how big a vector is, you can still use this with mathematical operations. If you Run the square root of my_long_vector, the Console will show you the square root of each element from 5 to 84.

***** Related Links *****
Power BI With R And RStudio: How To Get Started
Three Ways To Use R Script In Power BI
Objects And Object Classes In R: The Basics

Conclusion

Vectors are one of the building blocks of R. They’re similar to a range in Excel or column in Power BI. R vectors are more advanced compared to basic objects in R. You can perform simultaneous operations to an array of data in one go.

In the next tutorials, you’ll learn how to work with a whole data frame which will bring you rows and columns of data.

George

Related Posts