May 12, 2021 R language tutorial
Variables give us a named store that our program can operate. V ariables in the R language can store combinations of atomic vectors, atomic vector groups, or many Robjects. A valid variable name consists of letters, numbers, and points, or underscore characters. T he variable name begins with a letter or a point that is not followed by a number.
The name of the variable | Legitimacy | Reason |
---|---|---|
var_name2. | Effective | There are letters, numbers, dots, and underscores |
VAR_NAMEļ¼ | Invalid | There are characters '%'. O nly points (.) and underscore allowed. |
2var_name | Invalid | Starts with a number |
.var_name,
var.name |
Effective | You can use a (.) but the start point (.) should not be followed by a number. |
.2var_name | Invalid | The starting point is followed by a number that invalidates it. |
_var_name | Invalid | At the beginning, this is not valid |
You can assign values to variables using the left, right, and equal operators. Y ou can print the value of a variable using the print() or cat() function. The cat() function combines multiple items into continuous printouts.
# Assignment using equal operator. var.1 = c(0,1,2,3) # Assignment using leftward operator. var.2 <- c("learn","R") # Assignment using rightward operator. c(TRUE,1) -> var.3 print(var.1) cat ("var.1 is ", var.1 ," ") cat ("var.2 is ", var.2 ," ") cat ("var.3 is ", var.3 ," ")
When we execute the code above, it produces the following results -
[1] 0 1 2 3 var.1 is 0 1 2 3 var.2 is learn R var.3 is 1 1
Note - Vector c (TRUE, 1) has a mixture of logic and numeric classes. Therefore, the logical class is cast to a numeric class, making TRUE 1.
In the R language, the variable itself does not declare any data type, but rather gets the data type of the R-object assigned to it. S o R is called a dynamic type language, which means that we can change the data type of a variable over and over again when we use the same variable in our program.
var_x <- "Hello" cat("The class of var_x is ",class(var_x)," ") var_x <- 34.5 cat(" Now the class of var_x is ",class(var_x)," ") var_x <- 27L cat(" Next the class of var_x becomes ",class(var_x)," ")
When we execute the code above, it produces the following results -
The class of var_x is character Now the class of var_x is numeric Next the class of var_x becomes integer
To know all the variables currently available in the workspace, we use the ls() function. T he ls() function can also use patterns to match variable names.
print(ls())
When we execute the code above, it produces the following results -
[1] "my var" "my_new_var" "my_var" "var.1" [5] "var.2" "var.3" "var.name" "var_name2." [9] "var_x" "varname"
Note - It is an example output, depending on the variables declared in your environment.
The ls() function can use patterns to match variable names.
# List the variables starting with the pattern "var". print(ls(pattern = "var"))
When we execute the code above, it produces the following results -
[1] "my var" "my_new_var" "my_var" "var.1" [5] "var.2" "var.3" "var.name" "var_name2." [9] "var_x" "varname"
To point (.) T he variables at the beginning are hidden and can be listed using the "all.names s TRUE" parameter of the ls() function.
print(ls(all.name = TRUE))
When we execute the code above, it produces the following results -
[1] ".cars" ".Random.seed" ".var_name" ".varname" ".varname2" [6] "my var" "my_new_var" "my_var" "var.1" "var.2" [11]"var.3" "var.name" "var_name2." "var_x"
You can use the rm() function to remove variables. L et's delete the variable var.3. W hen printing, the value of the variable error is thrown.
rm(var.3) print(var.3)
When we execute the code above, it produces the following results -
[1] "var.3" Error in print(var.3) : object 'var.3' not found
All variables can be deleted together by using the rm() and ls() functions.
rm(list = ls()) print(ls())
When we execute the code above, it produces the following results -
character(0)