Creating Variables in R Language

Let’s look at ways of 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.

Syntax for creating R Variables

Types of Variable Creation in R:

  • Using equal to operators
      variable_name = value
     
  • using leftward operator
     variable_name <- value
     
  • using rightward operator 
     value -> variable_name

Creating Variables in R With Example

Let’s look at the live example of creating Variables in R:

R




# R program to illustrate
# Initialization of variables
 
# using equal to operator
var1 = "hello"
print(var1)
 
# using leftward operator
var2 <- "hello"
print(var2)
 
# using rightward operator
"hello" -> var3
print(var3)


Output

[1] "hello"
[1] "hello"
[1] "hello"

R Variables – Creating, Naming and Using Variables in R

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.

Similar Reads

Variables in R

R Programming Language is a dynamically typed language, i.e. the R Language Variables are not declared with a data type rather they take the data type of the R-object assigned to them....

Creating Variables in R Language

Let’s look at ways of declaring and initializing variables in R language:...

Nomenclature of R Variables

...

Important Methods for R Variables

The following rules need to be kept in mind while naming a R variable:...

Scope of Variables in R programming

R provides some useful methods to perform operations on variables. These methods are used to determine the data type of the variable, finding a variable, deleting a variable, etc. Following are some of the methods used to work on variables:...

Difference between local and global variables in R

...