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

Comprehensive Guide to Data Transformation and Prediction with R

This thread explores advanced topics in data analytics, focusing on building data pipelines, comparing SQL and R for data transformation, and applying predictive modeling techniques such as customer churn analysis and time series forecasting in R.

Market Basket Insights Using Association Rule Learning in R

A hands-on guided project to discover hidden patterns and relationships in retail transaction data using the Apriori algorithm in R.

Mastering Hierarchical Clustering with R: Dendrograms and Cluster Trees in Action

An in-depth, hands-on course designed to teach the practical application of hierarchical clustering in R, complete with real-world examples, to enhance advanced analytical skills.

Mastering Prescriptive Analytics with R: A Practical Guide

This project aims to teach the principles of prescriptive analytics and optimization through hands-on examples using the R programming language.

Mastering Data Manipulation in R with dplyr

A comprehensive guide to effectively manipulate and transform data using the dplyr package in R.

Mastering Random Forest Models for Business Applications

Learn how to harness the power of Random Forest models to tackle real-world business challenges.

Mastering Reusable Code and Analysis in R

A comprehensive guide to writing efficient, reusable code and performing analysis using the R language.

Forecasting Stock Price Movements Using Random Forest in R

A comprehensive guide to predicting stock price trends using Random Forest models in R.

Supply Chain Optimization Using Random Forests and R

A project aimed at optimizing inventory levels for a manufacturing company through predictive modeling using Random Forests in R.

Sales Forecasting for Retail Chain using Random Forest in R

This project aims to predict future sales for a retail chain using Random Forest algorithms implemented in R, aiding in informed supply chain and inventory management decisions.

Beginner’s Guide to R for Business Applications

Learn the fundamental concepts of the R programming language and how to apply them for business analytics and data-driven decision making.

Sentiment Analysis of Social Media Posts using Random Forest in R

This project aims to utilize a Random Forest model to classify social media comments as positive, negative, or neutral. By doing so, the company hopes to gain insights into customer sentiments towards its products and services.