Saturday, November 21, 2020

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.

  1. # Assignment using equal operator.  
  2. variable.1 = 124             
  3.   
  4. # Assignment using leftward operator.  
  5. variable.2 <- "Learn R Programming"     
  6.   
  7. # Assignment using rightward operator.     
  8. 133L -> variable.3             
  9.   
  10. print(variable.1)  
  11. cat ("variable.1 is ", variable.1 ,"\n")  
  12. cat ("variable.2 is ", variable.2 ,"\n")  
  13. 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:

R Programming Variables

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:

  1. variable_y<- 124  
  2. cat("The data type of variable_y is ",class(variable_y),"\n")  
  3.   
  4. variable_y<- "Learn R Programming"     
  5. cat("  Now the data type of variable_y is ",class(variable_y),"\n")  
  6.   
  7. variable_y<- 133L   
  8. 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:

R Programming Variables

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...