CodeInfoNet

Learn programming on the go!

Home » Using Variables In R

Using Variables In R

Overview

This blog is specifically for understanding the use of variables in R programming. After reading this blog you will be able to assign values, perform multiple arithmetic operations as well as understanding how to assign character values to variables.


Quick Recap

In previous article we learnt the data types in R programming. In case you have missed it, here is a link for the article Data types in R.

We covered the data types such as Int, double, char, complex and logical.

Let us know if the article is appropriate or if it needs any improvements. Mail us at admin@codeinfo.net or you can fill the feedback form at the end of this article.

In this article I am going to tell you about how to use variables to perform different arithmetic operations and storing values.

Note: Before continuing I would like to ask you to simultaneously perform the steps that I have given, because only reading the article won’t help. You must do the hands on in order to learn quickly. “Practice makes man perfect!!”

Let’s get started quickly!

Variables in R

In simple terms, variables are the entities which can change depending on the conditions, expressions. We can store values in the variables and we can perform different operations on them. Learn more about variables here

Some of the examples of operations that we can perform on variables are such as:

1. Arithmetic operations

2. Logical operations etc.

Let’s look at arithmetic operations:

Arithmetic Operations

Arithmetic Operations such as Addition, Subtraction, Division, Multiplication can be performed very easily with the variables in R.

1. Addition

Declare variables and store any value into them as given below:

a <- 20

b <- 10

Now if we perform any operation we have 2 parts as expression and the result. Hence, we need a variable to store the result of the addition

Let’s save the result in variable ‘c’ such as:

c <- a + b

Note: It is not mandatory to give spaces between the variables in R. R programming language completely ignores the spaces and hence spaces doesn’t matter at all. But for better visibility of the code, in any programming language, it is always good to add spaces after every variable when you are assigning value or retrieving result (Just don’t disturb the syntax! LOL).

Now we need to see our result and in any programming language we generally print the result either in console or display on other options based on programming language platform.

In general you might have heard of the functions such as printf, print, console.log, response.write etc. and many more. These functions are used to print the result/output in most of the programming languages.

But in R, you don’t have to write any of such function.

Then how we are going to display the result??

Solution

To display the output of the operation we just need to execute the variable in which we are storing the value of the expression.

In our case, we are storing the value in variable c.

Hence type c and execute by pressing Ctrl + Enter (on windows).

Now we got result in console!

Similarly, we can perform various arithmetic operations as well.

Let’s try the following

2. Subtration

var1 <- 201

var2 <- 100

output <- a – b

output 

(o/p- 101

3. Multiplication

a <- 20

b <- 10

result <- a * b

result 

(o/p - 200)

4. Division

x <- 200

y <- 10

z <- a / b

z
(o/p - 20)

Now let’s see another exciting use of a variable.

Let’s calculate the square root of any value stored in variable x

x <- 49

result <- sqrt(x)

result

In variable result you will get the value 7 as a square root of 49. Don’t worry about ‘sqrt()’ function right now. I will explain it in upcoming articles related to functions.

Up till now, we performed operations on integer variables. We can also play with character type of variables in R. Let’s see how:

Below is the snapshot showing the basic arithmetic operations that we performed above with variables:

Arithmetic Operations on Variables
Arithmetic Operations
Operations on Character type of variables

You already know how to store the value in a variable. It goes in same way for character variables as well.

Ch1 <- “Welcome”

Ch2 <- “To”

Ch3 <- “CodeInfo.Net”

chResult <- paste(ch1, ch2, ch3)

chResult

Again don’t worry about the paste() function right now. Still for your information, the paste()function will concatenate the values of those three variables as specified in the function call and you will get the result as “Welcome To CodeInfo.Net”. Isn’t it amazing?? Yes. Indeed!!

Below snapshot shows how to assign values to character variable

Assign value to Character variables
Assign value to Character variables

That’s it for now!

If you like this article please let me know. Also if you think there is a scope of improvement, still feel free to give me feedback.

Keep coding.

Feedback

[wpforms id=”22″]

Sumit Rajguru

Back to top