Nomenclature
A variable must follow the given rules when it is declared in R:
-
It should only contain letters, numbers, and dots or underscores.
-
It should not begin with a dot followed by a number (Example, .2arr).
-
It must not begin with a number (Example, 2arr).
-
It should not begin with an underscore (Example, _arr).
- It must not be a reserved keyword. (Example, TRUE, FALSE, etc.)
How to Initialize and Declare the Variables in R?
R does not have a command for declaring variables. When you assign a value to a variable, it is created. The variables in R can be assigned values using the leftward, rightward, and equal operators. Variable values can be printed using the print() or cat() functions. The cat() function combines many elements into a continuous print output.
Example:
# Using equal to operator
name1 = "John"
print(name1)
# Using the leftward operator
name2 <- "John"
print(name2)
# Using the rightward operator
"John" -> name3
print(name3)
Output:
[1] "John"
[1] "John"
[1] "John"
Syntax of Variables
Creating variables in R and giving them values requires the assignment operator, which can be either <- or =. The following is the standard syntax for generating variables in R:
variable_name <- value
or
variable_name = value
Here, 'value' is the information or expression you wish to put in the variable, and 'variable_name' is the name you give it.
Types of Variables in R
Variables in R can be classified into the following types based on the sort of data that you want to store.
Boolean Variables
It contains single-bit data that can only be TRUE or FALSE. TRUE denotes yes, while FALSE denotes no.
Example:
a = FALSE
print(a)
print(class(a))
Output:
[1] FALSE
[1] "logical"
We have declared the boolean variable a with the value FALSE here. Because Boolean variables belong to the logical class, class(a) yields "logical".
Integer Variables
It stores numeric data without any decimal values.
Example:
A = 25L
print(A)
print(class(A))
Output:
[1] 25
[1] "integer"
In this case, L represents an integer value. Because integer variables are members of the integer class in R, class(a) returns "integer".
Floating Point Variables
Numeric data with decimal values are stored in these variables.
Example:
x = 26.8
print(x)
print(class(x))
Output:
[1] 26.8
[1] "numeric"
Here, a floating-point variable with the name x has been created. The floating point variable is a member of the numerical class.
Character Variables
These variables store single-character data.
Example:
letter = "a"
print(letter)
print(class(letter))
Output:
[1] "a"
[1] "character"
In this case, we've generated a character variable called a letter. Class(letter) yields "character" because character variables are members of the character class.
String Variables
It stores data that contains more than one character. To express string data, we use double quotations.
Example:
str = "Harry is a cute boy!"
print(str)
print(class(str))
Output:
[1] "Harry is a cute boy!"
[1] "character"
Here str stores the string, and the class(str) returns "character" because strings are members of the character class.
Scope of Variables in R
The scope of a variable is the location where we can find a variable and access it if necessary. Variable scopes are classified into two types:
Global Variables
Global variables are variables that exist throughout a program's execution. It is changeable and accessible from anywhere in the program.
They are available for the complete duration of a program. Global Variables can be defined anywhere in the program, including outside of functions and blocks.
Example:
# global variable
global_var = 50
# global variable accessed inside a function
display = function(){
print(global_var)
}
display()
# Modifying the value of the global variable
global_var = 20
display()
Output:
[1] 50
[1] 20
The variable 'global_var' is declared at the top of the program outside of any functions. It indicates that it is a global variable that can be accessed or altered from anywhere in the program.
Local Variables
Local variables exist just within a certain section of a program, such as a function. They are released after the function call terminates. Local variables do not exist outside of the block in which they are declared. Hence cannot be accessed or utilized outside of that block.
Example:
func = function(){
# This variable is local to the
# function func(). It cannot be
# accessed outside this function
age = 21
print(age)
}
print("Age is:\n")
func()
Output:
Age is:
[1] 21
The variable 'age' is declared inside the 'func' function; hence it can be accessed in that function only.
Frequently Asked Questions
What is the confusion matrix in R?
A confusion matrix is used to evaluate the correctness of the model. It computes a cross-tabulation of observed and expected classes. This can be accomplished by calling the "confusionmatrix()" method from the "caTools" package.
What is a factor in R?
In R, factors are variables with only a few possible values. One of the most common uses of factors is in statistical modeling. Since categorical variables are treated differently in statistical models than continuous variables, storing data as factors guarantees that the modeling functions deal with such data correctly.
What is Principal Component Analysis in R?
Principal Component Analysis is a technique for reducing dimensionality. Many times, one observation is related to numerous dimensions (features), which causes a lot of chaos in the data. Hence it is vital to reduce the number of dimensions.
What is the rattle package in R?
Rattle is a popular R-based data mining GUI. It provides statistical and visual summaries of data. It converts data so that it can be easily modeled. Creates both unsupervised and supervised machine-learning models from the data. It visually displays model performance and scores fresh datasets for production deployment.
Conclusion
In R, variables are essential for programming and data analysis. They are fundamental components of R programming that enable efficient data storage, manipulation, and analysis. In this tutorial, we covered what a variable is and how to define a variable. We also discussed different types of variables along with their scope.
To better understand the topic, you can refer to R Programming Language, Data Structures in R Programming, and R- environment.
For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Python, Data Structures and Algorithms, Competitive Programming, System Design, and many more!
Head over to our practice platform, Coding Ninjas Studio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!