Variables in R Programming

A variable is created to reserve some memory spaces. The reserved memory space depends upon the type of data to be stored within the defined variable. The data is stored in a variable to perform various operations on that data, as per the requirement. The data stored in a variable can be manipulated to get the desired result.

Valid and Invalid Variable Name:

A variable name in R is valid if the first letter of the variable name is a letter or a dot not followed by a number and the name of the variable contains letters, numbers, dots and underline characters only. For all other cases, the name of a variable in R is invalid.

Example: Validity of a Variable Name:

Variable Name Valid/Invalid Explanation
7var_name Invalid In R programming, the name of a variable can not start with a numeric digit.
_var_name Invalid In R programming, the name of a variable can not start with an underscore(_).
var_nam Invalid In R programming, the name of a variable can not include any special character other than a dot(.) and underscore(_).
.5var_name Invalid In R programming, the name of a variable can not start with a dot(.) followed by a numeric value.
var_name Valid A variable name in R is valid if the first letter of the variable name is a letter or a dot not followed by a number and the name of the variable contains letters, numbers, dots and underline characters only.
var.name Valid A variable name in R is valid if the first letter of the variable name is a letter or a dot not followed by a number and the name of the variable contains letters, numbers, dots and underline characters only.
var_name2 Valid A variable name in R is valid if the first letter of the variable name is a letter or a dot not followed by a number and the name of the variable contains letters, numbers, dots and underline characters only.

Assignment of variables:

The leftward, rightward, and equal_to operators are used for the assignment of values to a variable in R.

Printing the value of the variable:

The print() and cat() functions are used to print the value of the variable in R.

Multiple values can be combined into a continued print output using the cat() function.

Example: Assignment of variables in R:

# Assignment using equal operator.
var1 = 543
 
# Assignment using the leftward operator.
var2 <- "Transformation is the key."
 
# Assignment using the rightward operator.
555L -> var3
 
print(var1)
 
cat ("var1 is ", var1,"\n")
cat ("var2 is ", var2,"\n")
cat ("var3 is ", var3,"\n")

 

The data type of variables in R:

Data Types are used to define the type of data that is to be stored in a variable. Depending on the type of data, the memory space is allotted to a variable by the operating system. Unlike programming languages, such as C++, R is a dynamically typed programming language. C++, on the other hand, is statically typed. In a dynamically typed programming language, the data type of a variable is checked during the execution of the respective statement. This also means that the data type of a variable can be changed multiple times in R programs. There is no static data type for a variable in R.

To check the data type of a variable in R:

In R programming, the class() function is used to serve this purpose.

Example: Verification of the data type of a variable in R:

var_1<- 666

cat("Data type of var_1 is ",class(var_1),"\n")

var_1<- "Data type of variables in R"

cat("Data type of var_1 is ",class(var_1),"\n")

var_1<- 200L

cat("Data type of var_1 is ",class(var_1),"\n")

 

Content Protection by DMCA.com