Skip to Main Content
Go to Penn Libraries homepage   Go to Guides homepage

R for Business Guide

Arithmetic Operators

R uses common arithmetic operators you may have seen elsewhere:

  • Plus: +
  • Minus: -
  • Times: *
  • Divided by: /​
  • To the power of: ^
  • The quantity: ()

Comparison Operators

Some of R's comparison operators differ from those in other languages:

  • Is equal to: ==
  • ​Is not equal to: !=​
  • Is greater than: >​
  • Is less than: <​
  • Is greater than or equal to: >=​
  • Is less than or equal to: <=

Logical Operators

Coordinate multiple comparison statements with logical operators:

  • And: &
  • Or: |
  • Not: !

The above operators work with statements that evaluate to TRUE or FALSE in R or with the values themselves:

  • True: TRUE or T
  • False: FALSE or F

Assignment Operators

R stores data in named objects.

Create an object by assigning data to a name. For clarity, it's conventional to assign with <-, e.g.:

dat <- c(1:5)
dat <- 1

More assignment operators exist:

  • Left assign: <- or =
  • Right assign: ->

Names are unique. If you assign to a new name, R creates a new object. If you assign to an existing name, the old object and its content disappear, replaced by your new assignment. Overwriting any object in R is that easy, so keep an eye on RStudio's auto-suggest box to see if a function or another object already has the name you plan to use.

RStudio, populating auto-suggest for 'dat...'

Retrieve the content of an object by calling the object's name.

RStudio, calling the object 'dat'

An object name is interchangeable with its content. Try it in your code.

RStudio, add an object to a vector

Business & Data Analysis Librarian

Profile Photo
Kevin Thomas
He/Him/His
Subjects: Statistics

Chat

Penn Libraries Home Search the Catalog
(215) 898-7555