Variables in R Programming

Variables in R Programming

In this guide we will discuss about Variables in R Programming. Variables are used to store the information to be manipulated and referenced in the R program. The R variable can store an atomic vector, a group of atomic vectors, or a combination of many R objects.

Language like C++ is statically typed, but R is a dynamically typed, means it check the type of data type when the statement is run. A valid variable name contains letter, numbers, dot and underlines characters. A variable name should start with a letter or the dot not followed by a number.

Name of variableValidityReason for valid and invalid
_var_nameInvalidVariable name can’t start with an underscore(_).
var_name, var.nameValidVariable can start with a dot, but dot should not be followed by a number. In this case, the variable will be invalid.
var_name%InvalidIn R, we can’t use any special character in the variable name except dot and underscore.
2var_nameInvalidVariable name cant starts with a numeric digit.
.2var_nameInvalidA variable name cannot start with a dot which is followed by a digit.
var_name2ValidThe variable contains letter, number and underscore and starts with a letter.

Assignment of variable

In R programming, there are three operators which we can use to assign the values to the variable. We can use leftward, rightward, and equal_to operator for this purpose.

There are two functions which are used to print the value of the variable i.e., print() and cat(). The cat() function combines multiples values into a continuous print output.

# Assignment using equal operator.  
variable.1 = 124             
  
# Assignment using leftward operator.  
variable.2 <- "Learn R Programming"     
  
# Assignment using rightward operator.     
133L -> variable.3             
  
print(variable.1)  
cat ("variable.1 is ", variable.1 ,"\n")  
cat ("variable.2 is ", variable.2 ,"\n")  
cat ("variable.3 is ", variable.3 ,"\n")  

When we execute the above code in our R command prompt, it will give us the following output:

Variables in R Programming

Data types of variable

R programming is a dynamically typed language, which means that we can change the data type of the same variable again and again in our program. Because of its dynamic nature, a variable is not declared of any data type. It gets the data type from the R-object, which is to be assigned to the variable.

We can check the data type of the variable with the help of the class() function. Let’s see an example:

variable_y<- 124  
cat("The data type of variable_y is ",class(variable_y),"\n")  
  
variable_y<- "Learn R Programming"     
cat("  Now the data type of variable_y is ",class(variable_y),"\n")  
  
variable_y<- 133L   
cat("   Next the data type of variable_y becomes ",class(variable_y),"\n")  

When we execute the above code in our R command prompt, it will give us the following output:

Variables in R Programming

Next Topic : Click Here

This Post Has One Comment

Leave a Reply