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

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.

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.

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:

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.

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.

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

George Mount
George Mount has over 10 years of analytics experience ranging from retail to healthcare. He is the author of the book "Advancing into Analytics: From Excel to Python and R."

## R vs Python -The Real Differences

In the world of data analysis and data science, you might be wondering which programming language is...

## R code Generator: Generate R Code From Plain Text

Are you tired of manually writing complex R code for your data analysis and visualization needs? Well,...

## grep & grepl R Functions Explained With Examples

A common task in data analysis is the need to find specific patterns within text data. Pattern...

## The Most Powerful Function Call In Power BI

Power BI is a powerful data visualization and business intelligence tool that allows users to connect...

## Creating A Jitter Plot Using ggplot2 In RStudio

The ggplot2 package is the most comprehensive way of building graphs and plots. Firms, like the New...

## How To Save & Load An RDS File In R

R is a popular programming language for data analysis, and it's often used to create and manipulate...

## How To Create A Lollipop Plot In RStudio

A lollipop plot, also known as a dumbbell plot, is a data visualization technique that combines a...

## Evaluating & Optimizing Code Performance In R

Optimizing R code can significantly improve the performance of R scripts and programs, making them run...

## Using R In Power BI: Creating Complex Visuals

In this blog, we will be continuing our series about the techniques to create complex custom visuals....

## Create Bivariate Visualizations In R Using ggplot2

Creating visualizations in R using ggplot2 can be a powerful way to explore and understand your data....

## Create A Histogram Using The R Visual In Power BI

Histograms are graphs that allow us to easily understand and visualize the distribution of a dataset....

## RStudio Help: Ways To Troubleshoot R Problems

Troubleshooting in R is the process of identifying and fixing problems or errors in your code. There...