Variables provide us with named storage that our programs can operate on. Variables in R language can store atomic vectors, groups of atomic vectors, or combinations of many Robjects. Valid variable names consist of letters, numbers, and dots or underscore characters. Variable names begin with letters or do not follow numbers.
Variable name | legality | reason |
---|---|---|
var_name2. | efficient | There are letters, numbers, dots and underscores |
VAR_NAME% | invalid | There are characters '%'. Only dots (.) and underscores allow. |
2var_name | invalid | Start with a number |
.var_name, |
efficient | You can use a point (.), but start point (.) and should not be followed by a number. |
.2var_name | invalid | The starting point is followed by a number to invalidate it. |
_var_name | invalid | Starting_This is invalid |
Variable assignment
The left, right and equal operators can be used to assign values to a variable. You can use the print() or cat() function to print the value of a variable. 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 above code, it produces the following result −
[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 logical and numerical classes. Therefore, the logic class is cast to a numeric class, making TRUE 1.
Data type of variable
In R language, the variable itself does not declare any data type, but instead gets the data type of the R-object assigned to it. So R is called a dynamic typed language, which means that we can change the data type of the variable again and again when using the same variable in the 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 above code, it produces the following result −
The class of var_x is character Now the class of var_x is numeric Next the class of var_x becomes integer
Find variables
To know all the variables currently available in the workspace, we use the ls() function. The ls() function can also use patterns to match variable names.
print(ls())
When we execute the above code, it produces the following result −
[1] "my var" "my_new_var" "my_var" "var.1" [5] "var.2" "var.3" "" "var_name2." [9] "var_x" "varname"
Note - It is a sample 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 above code, it produces the following result −
[1] "my var" "my_new_var" "my_var" "var.1" [5] "var.2" "var.3" "" "var_name2." [9] "var_x" "varname"
Variables starting with dot (.) are hidden and they can be listed using the "= TRUE" parameter of the ls() function.
print(ls( = TRUE))
When we execute the above code, it produces the following result −
[1] ".cars" "." ".var_name" ".varname" ".varname2" [6] "my var" "my_new_var" "my_var" "var.1" "var.2" [11]"var.3" "" "var_name2." "var_x"
Delete variables
You can use the rm() function to delete variables. Next we delete the variable var.3. When printing, the wrong value of the variable is thrown.
rm(var.3) print(var.3)
When we execute the above code, it produces the following result −
[1] "var.3" Error in print(var.3) : object 'var.3' not found
All variables can be deleted by using rm() and ls() functions.
rm(list = ls()) print(ls())
When we execute the above code, it produces the following result −
character(0)
This is the end of this article about summarizing the knowledge points of R language about variables. For more related R language variable content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!