# 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

## Ultimate Guide to R Programming For Data Analysis

An insightful thread covering R programming essentials, including data uploading, analytical patterns, visualization techniques, and leveraging R for effective business data analysis. Perfect for beginners and data professionals alike.

## Time Series Analysis of Stock Market Data with R

Analyze and predict stock prices using historical stock data with time series analysis techniques and ARIMA models.

## Time Series Analysis of Stock Market Data using R

A comprehensive project to perform time series analysis on stock market data using R, focusing on trend detection, seasonality, and forecasting.

## 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....

## 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...

## Evaluating & Optimizing Code Performance In R

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

## Communicating Research With R Notebook – Part 2

Today, we'll be continuing on the topic of communicating research using R Notebook. We'll continue...

## RStudio Help: Ways To Troubleshoot R Problems

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

## 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,...

## GGPLOT2 In R: Visualizations With ESQUISSE

In this tutorial, you’ll learn how to use the ggplot2 and esquisse packages to create visualizations in...

## The Most Powerful Function Call In Power BI

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

## Scatter Plot In R Script: How To Create & Import

In this tutorial, you’ll learn how to create a scatter plot visualization using an R script. This is...