How to add 2 numbers in R?

This recipe helps you add 2 numbers in R

Recipe Objective

How to add two numbers in R? The addition of two numbers is an arithmetic operation of adding the two numbers and storing the output in a vector. The input values can be pre-defined or can be user-defined. The addition operation can be done on a single number or a list of input values. sum () is the function used for performing the operation. This recipe performs the addition of two numbers using the + as well as the sum () function.

​Should I Learn Python or R for Data Science? Unlock the Answer

Step 1- Create 2 input vectors

x <- 10 y <- 20

Step 2- Add the vectors

Add the two input vectors and store the output value in a third vector

z <- x+y print(paste("Addition of two numbers is:",z))

"Output of the code is : Addition of two numbers is"  : 30 

Step 3- User defined input vectors

Addition of two numbers can also be done , with user defined values using the following syntax

a <- readline(prompt="enter the first input value : ") b <- readline(prompt="enter the second input value : ")

"Output of the line is": 
enter the first input value : 10
enter the second input value : 20

The user defined input values are character type , they are converted into integer type for performing the addition operation

a <- as.integer(a) b <- as.integer(b)

Step 4 - Add two user defined vectors

Adding two user defined input vectors and storing the output in a third vector

c <- a + b print(paste("Addition of user defined two numbers is:",c))

"Output of the code is : Addition of two numbers is" : 30 

Step 5- Using built in function sum()

x <- c(10,20) print(paste("Addition of two numbers is :",sum(x)))

"Output of the code is : Addition of two numbers is" : 30

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python