R variables and assignment
last modified November 19, 2026
A variable is a name for a value, and assignment is the act of giving a value
a name. R offers more forms of assignment than most languages: the arrow forms,
the equals sign, and the functions assign and get.
This article explains each form, the rules for valid names, and the functions
that list, test, and remove objects in the workspace.
Assigning a value
The usual assignment operator in R is <-, written as a
less-than sign followed by a hyphen. The expression on the right is evaluated
first, and the resulting value is stored under the name on the left. The
stored value is called an object. The next script assigns a number,
computes another number from it, and prints both.
x <- 10
y <- x * 2
cat("x =", x, "\n")
cat("y =", y, "\n")
The second assignment reads the current value of x, multiplies it
by two, and stores the result in y. Changing x later
would not change y, because the multiplication has already
happened. In RStudio, the keyboard shortcut for <- is
Alt plus - on Windows and Linux, and
Option plus - on macOS.
$ Rscript assigning.R x = 10 y = 20
Assignment returns a value
An assignment is an expression, not a statement, so it produces a value: the value that was assigned. The result is invisible, which means that the console does not print it automatically. Wrapping the assignment in parentheses makes the value visible. Assignments can also be chained, and then they are evaluated from right to left.
x <- 4
(y <- x * 3)
total <- doubled <- 7
cat("total =", total, "\n")
cat("doubled =", doubled, "\n")
The second line prints [1] 12 because the parentheses expose the
value. The chained assignment stores seven in doubled first and
then in total, so both names refer to the same number. The chain
is convenient for constants that belong together, but two separate lines are
easier to read when the values are unrelated.
$ Rscript returnvalue.R [1] 12 total = 7 doubled = 7
The equals sign
The equals sign also assigns a value at the top level of a session or script,
so x = 5 and x <- 5 have the same effect there.
The equals sign has a second job inside a function call: it binds a value to a
named parameter, as in mean(x = c(1, 2, 3)). The arrow has no
such second meaning, which is why most R code uses it for assignment.
x = 5
cat("x =", x, "\n")
print(mean(x = c(1, 2, 3)))
The first line assigns five to x. In the last line the same sign
does not create a variable called x; it tells mean
which argument receives the vector. The distinction matters in conditions,
where = is a syntax error and == must be used for
comparison.
$ Rscript equals.R x = 5 [1] 2
Right assignment
The arrow can point the other way. The form value -> name
assigns from left to right and is useful at the end of a long expression, where
the name can be written after the calculation has been read. It is the same
assignment written in the other direction.
12 -> answer "R" -> language cat(answer, "\n") cat(language, "\n")
Both names receive the value on their left. Right assignment is uncommon in
short scripts, but it appears in pipe-like code, where a long chain of
operations ends with -> result. Mixing the two directions in
one expression is legal but hard to read, so pick one direction per line.
$ Rscript rightassign.R 12 R
Superassignment
The operator <<- is the superassignment operator.
Instead of creating a name in the current environment, it searches the
enclosing environments for an existing name and assigns there, falling back to
the global environment when the name does not exist. An
environment is a container that maps names to values; the global
environment is the one that holds the objects created at the prompt, and every
function call gets its own environment.
Superassignment matters only inside functions, so the functions and scoping
articles later in the series return to it. For now the important point is the
difference: <- creates or replaces a name in the current
environment, while <<- assigns to an outer name instead of
a local one. The search starts in the parent environment and continues outward
until an existing name is found.
Names
A name, or identifier, may contain letters, digits, dots, and
underscores. It must begin with a letter or with a dot that is not followed by
a digit, so x2, total.sum, and
first_name are valid, while 2x and .2a
are not. Names are case-sensitive: value, Value, and
VALUE are three different objects.
first_name <- "Ada"
first.name <- "Grace"
First_Name <- "Barbara"
cat(first_name, first.name, First_Name, "\n")
value <- 10
Value <- 20
cat(value, Value, "\n")
print(make.names(c("2x", "a-b", "if", ".2a")))
The dot is an ordinary character in a name, not a member-access operator as in
some other languages. The function make.names repairs names that a
data import might produce: it adds an X in front of a name that
starts with a digit or with a dot and a digit, replaces a hyphen with a dot,
and appends a dot to a reserved word.
$ Rscript naming.R Ada Grace Barbara 10 20 [1] "X2x" "a.b" "if." "X.2a"
Reserved words
Some words are part of the syntax and cannot be used as names. They include
if, else, repeat, while,
function, for, in, next,
break, and the constants TRUE, FALSE,
NULL, Inf, NaN, and NA
together with its typed forms such as NA_integer_.
T and F are not reserved: they are ordinary objects
that start out as abbreviations for true and false. Assigning
T <- 0 is legal and then breaks every expression that relied on
the abbreviation. Write TRUE and FALSE in full, and
leave T and F alone.
Listing and removing objects
The function ls returns the names in the current environment,
sorted alphabetically. Names that begin with a dot are hidden from the listing
by default, in the same way that hidden files are omitted by a directory
listing. Passing all.names = TRUE shows them, and
exists tests whether a single name is defined.
a <- 1
b <- 2
.hidden <- 3
print(ls())
print(ls(all.names = TRUE))
rm(a)
cat("after rm:", ls(), "\n")
cat("a exists:", exists("a"), "\n")
rm(list = ls())
cat("objects left:", length(ls()), "\n")
rm accepts one or more names written directly, without quotes.
Its list argument takes a character vector of names instead, which
is why rm(list = ls()) removes every visible object at once. The
hidden object survives that call because ls() did not include it;
rm(list = ls(all.names = TRUE)) removes it too. Removing objects
frees memory only when no other name refers to them.
$ Rscript listing.R [1] "a" "b" [1] ".hidden" "a" "b" after rm: b a exists: FALSE objects left: 0
Assignment as a function call
Every operator in R is a function, and the assignment arrow is no exception.
The functions assign and get make the operation
explicit: assign takes the name as a string and the value as its
second argument, and get takes a name as a string and returns the
value. They are useful when the name is computed at run time or read from a
file.
assign("total", 42)
cat("total =", get("total"), "\n")
cat("exists:", exists("total"), "\n")
rm(total)
cat("exists after rm:", exists("total"), "\n")
The call assign("total", 42) has the same effect as
total <- 42. get("total") reads the value, and
exists("total") checks for it. A direct call such as
rm(total) looks different because rm does not
evaluate its argument in the usual way; it reads the written name instead. Both
forms are common, and the string form is the one to use inside a function that
receives the name as a variable.
$ Rscript assignget.R total = 42 exists: TRUE exists after rm: FALSE
Predefined objects
A fresh session already contains objects supplied by the base package.
pi holds the ratio of a circle's circumference to its diameter,
LETTERS and letters hold the alphabet in upper and
lower case, and month.abb and month.name hold the
month names in short and long form. They are ordinary objects, not reserved
words, so an assignment can mask them.
cat("pi =", pi, "\n")
cat(month.abb, "\n")
cat(LETTERS, "\n")
Masking pi with pi <- 3 is allowed, and every later
calculation in the session then uses three. The base value is still reachable
through its package, but the simplest fix is to remove the new object with
rm(pi). Short, familiar names are convenient precisely because
they are easy to remember, and that is also why accidental masking causes
confusing results.
$ Rscript constants.R pi = 3.141593 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Copying on assignment
An assignment copies the value at the moment it runs. If the name on the right is later given a new value, the name on the left keeps the old one. This is different from a reference, which would follow the changes of the original object.
x <- c(1, 2, 3)
y <- x
x <- c(9, 9, 9)
cat("x:", x, "\n")
cat("y:", y, "\n")
After the third line, x points to a new vector and y
still points to the original one. R uses a technique called
copy-on-modify: the two names may share memory while the values are
identical, and a copy is made only when one of them is modified. The observable
result is the same as an eager copy, but large vectors are not duplicated
without need.
$ Rscript copy.R x: 9 9 9 y: 1 2 3
Next steps
Now that values can be stored and named, the next article surveys the data types that those values can have. The operators article shows how to combine values with arithmetic, comparison, and logic, and the vectors article later in the series explains how one name can hold many values.
Source
The R Language Definition: objects
An Introduction to R: simple manipulations
R documentation: assign and get
In this article we have assigned values with <-, =,
and ->, seen that assignment returns an invisible value, chosen
valid names, and listed and removed objects with ls,
exists, and rm. We also called assign
and get directly and compared assignment with copying. The series
continues with R data types.
Author
List all R Language tutorials.