Assignment and Special Operators
Explore >, %in%, and the pipe operator in R.
Assignment in R
R has several ways to assign values to variables. The most idiomatic is <-, which is the preferred style. You will also encounter = for assignment, though it behaves slightly differently in some contexts.
# Both assign a value, <- is preferred
name <- 'Alice'
age = 30
cat('Name:', name, '
')
cat('Age:', age, '
')Left Arrow <- vs Equals =
<- always creates a variable in the current environment. = can also assign, but inside function calls it sets named arguments rather than creating a variable. This distinction matters.
# <- creates a variable outside function calls
x <- 42
# = inside function call sets argument, does NOT create x
mean(x = c(1, 2, 3)) # x is an arg here, not a variable
# But <- inside a function call DOES create a variable
mean(y <- c(1, 2, 3)) # y is now also in workspace
cat('y was created:', y, '
')All lessons in this course
- Arithmetic and Comparison Operators
- Logical Operators and Boolean Logic
- Assignment and Special Operators
- Operator Precedence and Complex Expressions