Variables
Published Jan 9, 2022
Contribute to Docs
A variable is used to store data that can be accessed later by subsequent code. In R, there are no variable “declaration” commands. Instead, they are created with the assignment operator, <-
(The more familiar assignment operator, =
, can be used instead, but is discouraged).
# This creates a variable containing the string "value"variable <- "value"
The assignment operator can be chained together to initialize multiple variables at once:
var1 <- var2 <- var3 <- 0
In R, writing a defined variable name by itself on a line has the effect of printing its associated value:
score <- 100# This line:score # Output 100# Has the same effect as this line:print(score) # Output 100
Variable Names
R variables must adhere to the following naming conventions:
- The name can be a combination of letters, digits, period(.) and underscore(_).
- It must start with a letter or a period.
- If it starts with a period, the second character cannot be a number.
- It cannot start with a number or an underscore.
- Variable names are case-sensitive.
- Reserved words (
TRUE
,FALSE
,print
, etc.) cannot be used as variable names.
Examples
The following code snippet shows examples of valid variable names in R:
foo <- 1Foo <- 2 # A different variable from "foo" above.bar <- TRUEfoo_bar <- "value"3.5 -> Foo123.4 # Rightward assignment.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn R on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn R
Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.Beginner Friendly14 hours