 # Variables in R Programming

January 13, 2022, Learn eTutorial
807

In our previous tutorial (Syntax and Basics for R Programming) we had an insight into a simple hello world program that gives you a brief idea of how to use RStudio, basic syntax, and how comments are written in R. This tutorial takes you to one of the fundamental basics of R i.e. the variables and constant concept in R. If you have any prior knowledge to python, Go (Python, Go) programming languages it will be a great advantage, within a short time you will be masters in R. The others don’t get panic R is a simple language that anybody can learn like a toddler learning to walk. So let’s jump to our tutorial and walk together.

## What are variables in R?

Variables are reserved memory locations to store data values or vectors or a list of values or a dataset or object in R. Like in all other programming languages, there should be some space inside memory locations that can hold the different values that are being used in R programming. These memory locations are represented by a name that is called later in the program for referring that address space and using the corresponding value contained by it. Whenever a variable is created in the R program corresponding memory location to store the value contained by that variable is reserved. In other words, a memory location is reserved while creating a variable.

Once a variable is created during the time of execution of the R program it identifies the datatype (type of data stored inside variable may be an integer (3), float (3.9), string (“R”)) of variable and allocates with a corresponding memory location. Note : Just understand the concept of variables in R ignore the syntax of variables in the picture you are going to learn that in upcoming sections of this tutorial.

## How to declare and initialize variables in R?

In the R programming language, three types of operators support in declaration and initialization of variables. The three operators are as follows

1. Assignment Operator (=)
2. Leftward Operator (<-)
3. Rightward Operator(->)

The syntax for assigning variables with values using the above operators.

``````
//Assignment Operator (=)
<variable_name>     =    <value >

//Leftward Operator (<-)
<variable_name>    <-    <value>

//Rightward Operator (->)
<value>     ->    <variable_name>

```
```

### Program to declare and initialize in R

``````
site = "Welcome to Learn eTutorials"  #using  = operator
print(site)

language <- "R programming  "         # using <- operator
print(language)

"Variables in R" -> Tutorial                #using -> operator
print(Tutorial)
```
```

Output:

```
 "Welcome to Learn eTutorials"
 "R programming  "
 "Variables in R"
```

In the above program, a variable `site` is created of string data type. The value “Welcome to Learn eTutorials” is assigned to site variable using “`=`” equal to or assignment operator. Similarly, another variable language is created which holds the value "R programming " .In these lines of codes leftward operator (“` <-` ”) assigns value to variable language. In the last lines of codes, you can see the program begins with initializing value "Variables in R" followed by a rightward operator “`->`” which assigns value to the variable Tutorial.

Let us look at the snippet to see how these codes really works ## How to write variable code in R Script?

Let us begin from the scratch with the classic hello world program. You are going to create a variable hello world of string datatype and print the resultant statement. In order to edit our code easily,  we will begin by familiarizing it with RStudio together with variables.

1. Open up an R script file. 2. Click the R Script file which pops up with a new panel where you can easily start writing your codes. Create a variable named VARIABLE that store a value “hello world!!!” of string data type .In R programming language to assign a value to a variable we use a different operators which we are going to discuss in this tutorial later. Let us see a simple example using “=” assignment operator that assigns value “hello world!!!” to VARIABLE and print () function displays output.

### Program to store hello world!!! in VARIABLE

``````
VARIABLE = "Hello world"
print(VARIABLE)
```
```
3. Write the above code in the R Script file panel and Run the code. Click the Run label to the right of your code. 4. After clicking the Run open up the console with code and the corresponding output. 5. Type VARIABLE and run the code to observe the change. ## Naming Convention in R

The following are the naming conventions followed in R programming language

1. Variable names only contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character underscore  ‘_ ’.Example  site_123, Site, site
2. Variables are case-sensitive. Example SITE, Site, site are variables of different types & values.
3. Keywords are not used as Variables. Example if,NULL,TRUE,FALSE
4. Variable names are not allowed to begin with a digit. Example 1language
5. Variable names are not allowed to begin with underscore they are unexpected tokens that cause an error (bug) in the program. Example _language
6. Variable names can begin with a dot. Example    .site_123
7. In R, dot (.) and underscore (_) symbols are only allowed for variable declaration. Example Language\$1, Language#\$1
8. Variable names can be a mix of uppercase and lowercase letters. Example Variable Name, varaibale_name, VARIABLE, variable name.

Valid Convention Invalid Conventions Description
site_123, Site, site,SiteName,Sitename   Available nomenclature
Language_1 _language, Language\$1 Beginning with underscore (_) and symbols other than dot, _ not allowed.
Language1 1language Beginning with digit is not allowed
.site_123 .123_site Beginning with dot is allowed but dot followed by digits are not allowed.

### Program to show naming conventions in R

``````
VARIABLE = "Hello world 1"
print(VARIABLE)

var1 = "Hello  world 2 "
print(var1)

variable_1 = "Hello  world 3"
print(variable_1)

VariableName = "Hello  world 4"
print(VariableName)

varaible_name ="Hello  world 5"
print(varaible_name)

_123variable = "Hello  world 6"
print(_123variable )
```
```

Output:

```
 "Hello world 1"] "Hello  world 2 "
 "Hello  world 3"
 "Hello  world 4"
 "Hello  world 5"
> print(_123variable )
Error: unexpected input in "print(_"
>

```

Look at the snippet to visualize the code for better understanding. ## What is the Scope of variables in R?

The scope is defined as a lifetime of a variable. A scope is an area under which a variable is alive or applicable. Consider there is a function called `sample()`. Inside the function `sample()` there is a variable called local_variable. The scope of the variable local_variable is within the function `sample()` only. This means the lifetime of local_variable is within that function. Suppose any other function defined outside the `sample()` function tries to access this variable declared local to `sample()` function, it cannot access it. Because the variable local_variable is not visible outside the block. This is what is meant by the scope of a variable. Whereas a variable declared outside the sample() function known as global_varaiable is easily accessible within the function as well as outside the function.

Note: A scope is a region or segment where a variable is declared, initialized, and used. When this region or block terminates from its execution the variable gets destroyed automatically.

Variable scope types Description
Global variables Variables that are accessible and visible from any part of the program. The lifetime of a variable is throughout program execution. Variables can be declared inside or outside of a function. Variables can be modified from any region of code may be from inside or outside of a function.
Local variables Variables that are accessible only within a function or code of segments. The lifetime or scope of a variable is limited within that function. Variables are locally declared inside a function. Variables can be modified inside the function only.

## How to declare Global variables in R?

In the below-given program, a variable named global_variable is declared with an initialized value of 45. The program contains a function `sample()`, inside the function is provided with the print statement for global_variable which displays the output as shown below. Later in the program outside the function `sample()`, the global_variable  value is updated with “hello”.

The example proves that the scope of the global variable is inside as well as outside of a function from where the globally declared variables are accessible and visible.

### program to show global variable declaration

``````
# R program to declare  global variables

# global variable
global_variable = 45

# global variable accessed from a function sample()
sample = function(){
print(global_variable)
}
sample()

# modified  global variable value
global_variable= "hello"
sample()

```
```

Output:

```
 45
 "hello"
```

## How to declare Local variables in R?

In the same program we discussed earlier here a local_variable is declared and assigned with the value “hello I am local variable" inside the function `sample()`. The program executes and displays the value and later the value of local_variable is modified with "hello my value got modified" within the function `sample()` itself.

Local variables are only locally accessible and can be modified only within a function where it is defined.

### program to show local variable declaration

``````
# R program to declare  local variables

# local variable accessed within a function sample()
sample = function(){
local_variable <- "hello i am local variable"
print(local_variable)

# modified local variable value
local_variable  <-"hello my value got modified"
print(local_variable)

}
```
```

Output:

```
 "hello i am local variable"
 "hello my value got modified"
```

## What are the methods used by variables in R?

Like other programming languages, R programming also has a rich set of built-in functions also called methods. Methods are functions that are already defined to perform a specific task that the user requires. Everything in R is done through a method or function. For instance, print() is a function used to print the given text on the console. Similarly, R has hundreds and hundreds of methods to perform a designated task. Here in this section, we are focusing on some of the common methods associated with variables, their usage, and syntax. See below table :

Method Description Syntax
class() Determines the data type of variables class(variable_name)
Is() To check currently available variables Is(variable_name)
rm() To remove/delete a variable rm(variable_name)
paste() Concatenate elements paste(varaible_1,varaible_2)

## How to identify the data type of a variable in R?

In the R programming language data type of variables can be determined by using a built-in function supported by the R package or standard libraries. The built-in function or method that supports identifying data type of variable is `class()`.

``````
class(variable_name)
• Keyword class defines class() to identify data type of variable
```
```

### Program to identify data type of a variable in R

``````
site = "Welcome to Learn eTutorials"  #using  = operator
print(site)

language <- "R programming  "         # using <- operator
print(language)

"Variables in R" -> Tutorial                #using -> operator
print(Tutorial)

cat("The class of variable site is ",class(site),"\n")

cat(class(language),"\n")

cat("The class of Tutorial is ", class(Tutorial) , "\n ")
```
```

Output:

```
 "Welcome to Learn eTutorials"
 "R programming  "
 "Variables in R"
The class of variable site is  character
character
The class of Tutorial is  character
```

## How to check available variables in R?

In the R programming language, the available variables are determined using a built-in function called `ls().`

``````
ls(variable_name)
• Keyword Is  defines  ls() function to check available variables in R program
```
```
``````
"language" "site.1"   "site.2"   "Tutorial"
```
```

These are the available or alive variables displayed by the below program during the execution of the program.

### Program to check available variables in

``````
site.1 = "Welcome to Learn eTutorials"  #using  = operator
print(site.1)

site.2 = "Learn eTutorials"
print(site.2)

language <- "R programming  "         # using <- operator
print(language)

"Variables in R" -> Tutorial                #using -> operator
print(Tutorial)

print(ls())      #To find available variables
```
```

Output:

```
 "Welcome to Learn eTutorials"
 "Learn eTutorials"
 "R programming  "
 "Variables in R"
 "language" "site.1"   "site.2"   "Tutorial"
```

## How to remove/delete a variable in R?

In the R programming language, the unnecessary variables or variables of no functionality can be removed or deleted from a program by using a built-in function called `rm()`.

``````
rm(variable_name)
• Keyword rm defines rm() built in function that removes a variable.
```
```

These are the available or alive variables displayed by the below program during the execution of the program.

### Program to remove/delete a variable in R

``````
site.1 = "Welcome to Learn eTutorials"  #using  = operator
print(site.1)

site.2 = "Learn eTutorials"
print(site.2)

language <- "R programming  "         # using <- operator
print(language)

"Variables in R" -> Tutorial                #using -> operator
print(Tutorial)
```
```

Output:

```
 "Welcome to Learn eTutorials"
 "Learn eTutorials"
 "R programming  "
 "Variables in R"
```

Let us add the below given built-in function `rm()` to delete a variable, where the parentheses enclose the name of a variable to be removed from the program.

``````
rm(site)
print(site)
```
```

When these lines of codes are added the variable named `site` gets removed from the program. You can see the corresponding output below after the execution of the program.

```
 "Welcome to Learn eTutorials"
 "Learn eTutorials"
 "R programming  "
 "Variables in R"
Warning message:
Execution halted
```

## How to concatenate variables in R?

R programming language supports a built-in function known as a `paste()` to concatenate one or more variables in a program. Two variables A & B are declared with values "Learn R in" & "Learn eTutorials" respectively. In this program, values are assigned to variables A & B using a leftward operator.

### program to concatenate variables in R

``````
A <- "Learn R in"
B <- "Learn eTutorials"

paste(A, B)
```
```

When the program gets executed the built-in method paste() concatenate the two variables A & B to produce the output as shown below

```
 "Learn R in Learn eTutorials"
```

## How to assign same value to multiple variables?

R supports single line assignment for multiple variables with same values. The three variables named first_variable , second_variable ,third_variable is assigned with same value using leftward operator with value "helloo it is easy right!!!!"

### program to assign multiple variables value

``````
# "Multiple value in single line"
first_variable <- second_variable <- third_varaible <- "helloo it is easy right!!!!"

# Print  values of variables
cat ("The first variable is ",first_variable,"\n")
cat ("The first variable is ",second_variable,"\n")
cat ("The first variable is ",third_varaible,"\n")
```
```

Output:

```
The first variable is helloo it is easy right!!!!
The first variable is helloo it is easy right!!!!
The first variable is helloo it is easy right!!!!
```

## What are the types of variables in R?

R supports two types of variables:

1. Numerical: Numerical or quantitative variables stores values to perform arithmetic operations like add, sub, div, mul or take averages, and so on. Numerical variables are further classified as continuous and discrete.
• Continuous variables can be measured types like height which takes infinite values within a given range.
• Discrete numeric variables are specific sets of values that can be counted.
2. Categorical: Categorical or qualitative variables take a  limited number of distinct categories.