Tutorial | R | Operators

R Has So Many %/>* Operators!

Relax, it’s just some R operators stuck together

Drew Seewald
9 min readJan 4, 2023

--

A lot of operations in R can be performed just by using operators. Have you ever used R as a calculator? Assigned a value to a variable? Written a for loop? Strung some tidyverse functions together? If you answered yes to any of the above, congrats, you’ve already used some operators. The most common operators are for assignment, math, piping, and control flow. Let’s dive in!

Assignment Operators

  • <- The left assignment operator. Use it to assign the value on the right to the variable on the left.
# <- Examples

variable <- "some text"

x <- 5
  • = The equals operator. Mainly used for setting arguments inside of function calls. It can also be used to assign values to variables, but most people use the left assignment operator (<-) for that.
# = Examples

lm(formula="y~x", data=cars)

#=====================

plot(x=x, y=y)

#=====================

# This works, but most people use <- instead of =
x = 5
  • -> The right assignment operator. Use it to assign the value on the left to the variable on the right. It’s pretty rarely used, but can sometimes make more sense when reading code with a…

--

--