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 variable | Validity | Reason for valid and invalid |
---|---|---|
_var_name | Invalid | Variable name can't start with an underscore(_). |
var_name, var.name | Valid | Variable can start with a dot, but dot should not be followed by a number. In this case, the variable will be invalid. |
var_name% | Invalid | In R, we can't use any special character in the variable name except dot and underscore. |
2var_name | Invalid | Variable name cant starts with a numeric digit. |
.2var_name | Invalid | A variable name cannot start with a dot which is followed by a digit. |
var_name2 | Valid | The 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.
When we execute the above code in our R command prompt, it will give us the following output:
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:
When we execute the above code in our R command prompt, it will give us the following output:
No comments:
Post a Comment