top of page

R Variables

A variable is a memory allocated for the storage of specific data and the name associated with the variable is used to work around this reserved block. The name given to a variable is known as its variable name. Usually a single variable stores only the data belonging to a certain data type. The name is so given to them because when the program executes there is subject to change hence it varies from time to time.

Declaring and Initializing Variables in R Language

R supports three ways of variable assignment:

  • Using equal operator- operators use an arrow or an equal sign to assign values to variables.

  • Using the leftward operator- data is copied from right to left.

  • Using the rightward operator- data is copied from left to right.

Creating Variables in R

# R program to illustrate
# Initialization of variables

# using equal to operator
var1 = "codeswithpankaj"
print(var1)

# using leftward operator
var2 <- "codeswithpankaj"
print(var2)

# using rightward operator
"codeswithpankaj" -> var3
print(var3)

Output

[1] "codeswithpankaj"
[1] "codeswithpankaj"
[1] "codeswithpankaj"

Important Methods for R Variables


class() function

This built-in function is used to determine the data type of the variable provided to it. The R variable to be checked is passed to this as an argument and it prints the data type in return.

Syntax

class(variable) 

Example

var1 = "codeswithpankaj"
print(class(var1))
[1] "character"

ls() function

This built-in function is used to know all the present variables in the workspace. This is generally helpful when dealing with a large number of variables at once and helps prevents overwriting any of them.

Syntax

 ls() 

Example

# using equal to operator
var1 = "codeswithpankaj"

# using leftward operator
var2 <- "codeswithpankaj"

# using rightward operator
"codeswithpankaj" -> var3

print(ls())

Output:

[1] "var1" "var2" "var3"

rm() function

This is again a built-in function used to delete an unwanted variable within your workspace. This helps clear the memory space allocated to certain variables that are not in use thereby creating more space for others. The name of the variable to be deleted is passed as an argument to it.

Syntax

 rm(variable) 

Example

# using equal to operator
var1 = "codeswithpankaj"

# using leftward operator
var2 <- "codeswithpankaj"

# using rightward operator
"codeswithpankaj" -> var3

# Removing variable
rm(var3)
print(var3)

Output

Error in print(var3) : object 'var3' not found
Execution halted 

Scope of Variables in R programming

Global Variables

Global variables are those variables that exist throughout the execution of a program. It can be changed and accessed from any part of the program.

As the name suggests, Global Variables can be accessed from any part of the program.

  • They are available throughout the lifetime of a program.

  • They are declared anywhere in the program outside all of the functions or blocks.

Declaring global variables

Global variables are usually declared outside of all of the functions and blocks. They can be accessed from any portion of the program.

# R program to illustrate
# usage of global variables

# global variable
global = 5

# global variable accessed from
# within a function
display = function(){
print(global)
}
display()

# changing value of global variable
global = 10
display()

Output

[1] 5
[1] 10

In the above code, the variable ‘global’ is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.


Local Variables

Local variables are those variables that exist only within a certain part of a program like a function and are released when the function call ends. Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.

Declaring local variables

Local variables are declared inside a block.

# R program to illustrate
# usage of local variables

func = function(){
# this variable is local to the
# function func() and cannot be
# accessed outside this function
age = 18
print(age)
}

cat("Age is:\n")
func()

Output

Age is:
[1] 18



Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Comments


bottom of page