R - Variables
Variable is a given name to a reserved memory location. When a variable is created in the program, it reserves some space in the memory to store value(s) and the interpreter allocates memory for the given variable based on its datatype.
Assigning Value to a Variable
R does not require to declare a variable or its data type. The data type of a variable is set when a value is assigned to it. To assign a value(s) to the variable, assignment operator (=, <-, <<-, -> or ->>) is used.
#store number in the variable 'x' x <- 15 print(x) #store text in the variable 'y' y = "Hello" print(y) #store vector in the variable 'z' z <<- c(1, 2, 3) print(z)
The output of the above code will be:
[1] 15 [1] "Hello" [1] 1 2 3
Data type of a Variable
In R, when a new value is assigned to the variable, the old value and its datatype will be overwritten by new value and its datatype. The check the data type of a variable, class() function can be used.
x <- 15 print(x) print(class(x)) cat("\n") x <- 50L print(x) print(class(x)) cat("\n") x <- 'Hello' print(x) print(class(x))
The output of the above code will be:
[1] 15 [1] "numeric" [1] 50 [1] "integer" [1] "Hello" [1] "character"
Print Variable
The value of the variable can be printed on the screen using print(), cat() or sprintf() function. cat() or sprintf() allows to combine multiple items into a continuous print output.
name <- "John" age <- 25 #printing using print() function print(name) print(age) #printing using cat() function cat(name, " is ", age , " years old.\n") #printing using sprintf() function sprintf("%s is boy who is %d years old.", name, age)
The output of the above code will be:
[1] "John" [1] 25 John is 25 years old. [1] "John is boy who is 25 years old."
Variable Name
There are some reserved keywords in R which cannot be used as variable name. Along with this, rules for creating variable name in R are listed below:
- It must start with a letter or dot(.) character. If it starts with dot(.), it should not be followed by a number.
- It cannot start with a number.
- It can only contains alpha-numeric characters, dot (.) and underscores (A-Z, a-z, 0-9, dot(.) and _ ).
Please note that R is a case-sensitive language. Hence, variables in R are also case-sensitive.
Global Variable
If a variable is created outside a function, it is called global variable. A global variable can be used anywhere, inside the function and outside the function.
Example
In the example below, a global variable MyStr is created and used inside the function to print it.
MyStr <- "Hello World!" #defining MyPrint function MyPrint <- function(){ print(MyStr) } MyPrint()
The output of the above code will be:
[1] "Hello World!"
If a variable with same name is created inside the function, it will be a local variable and can be used inside the function only. Any operation performed on local variable will not change the global variable.
Example
In the example below, a variable MyStr is assigned different values inside and outside the function. When the variable is accessed outside the function, it takes global value and its global value remains unaffected by any operation done with local variable.
MyStr <- "Hello World!" #defining MyPrint function MyPrint <- function(){ MyStr <- "Hello John!" print(MyStr) } #variable accessed inside function MyPrint() #variable accessed outside function print(MyStr)
The output of the above code will be:
[1] "Hello John!" [1] "Hello World!"
Global assignment operators
To access or create a global variable inside a function, following operators can be used:
- <<- : left global assignment operator
- ->> : right global assignment operator
Example
In the example below, (<<-) operator is used to modify and create global variable inside a function.
MyStr <- "Hello World!" #defining MyPrint function MyFunction <- function(){ #accessing a global variable MyStr <<- "Hello John!" #creating a new global variable #it is created after function call MyMessage <<- "R programming is fun" } MyFunction() #accessing variables outside function print(MyStr) print(MyMessage)
The output of the above code will be:
[1] "Hello John!" [1] "R programming is fun"
Finding Variables
To know all the variables currently available in the workspace, ls() function can be used. Please consider the following example:
var1 <- 10 var2 <- "Hello" .var3 <- c(1, 2, 3) MyStr <- "Hello World" #List all visible variables print(ls()) #List all visible variable #containing pattern "var" print(ls(patter="var")) #variable starting with dot(.) are #hidden, to list those variables #all.name = TRUE must be used print(ls(all.name = TRUE))
The output of the above code will be:
[1] "MyStr" "var1" "var2" [1] "var1" "var2" [1] ".var3" "MyStr" "var1" "var2"
Deleting Variable
A variable can be deleted using rm() function. Please consider the example below.
MyStr <- "Hello World!" print(MyStr) #deleting the variable rm(MyStr) #trying to print the variable #after deleting print(MyStr)
The output of the above code will be:
[1] "Hello World!" Error in print(MyStr) : object 'MyStr' not found Execution halted
rm() function can be combined with ls() to delete set of variables. Please consider the example below:
var1 <- 10 var2 <- "Hello" var3 <- c(1, 2, 3) MyStr <- "Hello World" #List all variables print(ls()) #Delete all variables with pattern "var" rm(list = ls(patter="var")) #List all variables print(ls())
The output of the above code will be:
[1] "MyStr" "var1" "var2" "var3" [1] "MyStr"