Introduction
In this article, I am going to explain about variables and constants in R. I will demonstrate how to define variables, declare variables and how to define constants and use predefined constants in R language.
Variables in R
In R, we can store data in a memory location and these memory locations have a name: They're called variables. Each and every variable in R can have different name which is referred to as an identifier and these identifiers can store values of different datatypes.
There are different ways to define a variable in R which are:
- In R, a variable always starts with a letter or with a period. A variable if started with a dot cannot be succeeded by a number.
- Variables cannot be created with keywords which are already predefined in R; that is keywords which are reserved, as their names or identifiers.
- A variable in R can be defined using just letters or an underscore with letters, dots along with letters. We can even define variables as a mixture of digits, dot, underscore and letters.
In R, a few instances of names of variables that are relevant are name, Var, var_1, .var, var.1
In R, a few instances of names of variables which are irrelevant are 5var, var@a, _sub, FALSE, .2ab.
From the above example, we can see that in R, to define a variable as a legitimate name of a memory location, we can use underscore at the beginning of the variable name or dot can also be used to separate letters from numbers in variable name. Various examples can be considered such as var.1, var.a, var_1.
Assigning values to variables
In R, assigning values to a variable can be specified or achieved using the syntax of left angular brackets signifying the syntax of an arrow. After assigning values to a variable, these values can be printed using the predefined cat() function and print() function. To integrate multiple statements assigned to a single variable and separated by double quotation marks and commas, we can use function of cat().
Consider the example given below,
> a = c(1,4,3,2)
> b.6 <- c("Defining functions in","R Language")
> x_y <- 34
>
> a
[1] 1 4 3 2
>
> b.6
[1] "Defining functions in" "R Language"
>
> x_y
[1] 34
>
Therefore variable assignment can be done using any of the syntaxes discussed above to assign a variable with a value of different datatypes.
Different datatypes of variables
Once we declare a variable in R to be of a particular datatype and run the command on R console to get the output of variable, then the same variable can be assigned over and over again with a different value having a different datatype. This shows the dynamic nature of R programming language.
Consider the following example,
>
> a <- ' Greetings'
> cat("Variable a has a class ",class(a),"\n")
Variable a has a class character
>
> x <- 12
> cat("Variable x has a class ",class(a),"\n")
Variable x has a class numeric
>
> name <- 'Sam Hopkins'
> cat("Variable name has a class ",class(name),"\n")
Variable name has a class character
Displaying variables in directory
Through the function ls(), the name of all the functions and variables or we can say objects defined within a particular workspace can be obtained in detail. In order to extract a particular from workspace, pattern matching attribute can be used to list the details of the exact variable from a workspace in R.

Join the conversation! Your thoughts help the community grow.