The different memory locations where data resides are called objects. To access such an object it must have a name, and such a named object is called a
Variable
.
The information stored inside an object is called as a value. Of course the machine itself typically has no idea how to interpret the information it contains, but the program certainly needs to know. For this reason a variable has a specific type. And it’s this type that constrains the use of that variable, such that only operations appropriate to the type may be performed.
The variables can be assigned with equal operator or leftward or rightward. The following is an example.
[c]
# Assignment using equal operator.
var.1 = c(0,1,2,3)
# Assignment using leftward operator.
var.2 <- c("learn","R")
# Assignment using rightward operator.
c(TRUE,1) -> var.3
print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
[/c]
In the above example, print() and cat() functions are used to display the results, but cat() function will combine different items.
Output: Now compile the code result will be as follows.
[c]
> print(var.1)
[1] 0 1 2 3
> cat ("var.1 is ", var.1 ,"\n")
var.1 is 0 1 2 3
> cat ("var.2 is ", var.2 ,"\n")
var.2 is learn R
> cat ("var.3 is ", var.3 ,"\n")
var.3 is 1 1[/c]
Every variable will have data type to store the variable value, the following is an example.
[c]var_x <- "SPLESSONS"
cat("The class of var_x is ",class(var_x),"\n")
var_x <- 92.5
cat(" Now the class of var_x is ",class(var_x),"\n")
var_x <- 32L
cat(" Next the class of var_x becomes ",class(var_x),"\n")[/c]
Output: Now compile the code result will be as follows.
[c]
> cat("The class of var_x is ",class(var_x),"\n")
The class of var_x is character
>
> var_x <- 92.5
> cat(" Now the class of var_x is ",class(var_x),"\n")
Now the class of var_x is numeric
>
> var_x <- 32L
> cat(" Next the class of var_x becomes ",class(var_x),"\n")
Next the class of var_x becomes integer
[/c]