Codecademy Logo

Basic Syntax and Variable Types

Print Cheatsheet

Basic Syntax in R

R uses symbols to represent:

  • math operations (+, -, *, and / and follows the order of operations)
  • logical operators (>, >=,<, <=, ==, and !=)
  • joining logical operators (&, &&, | and ||)
  • object assignment (<- or =)

The names of objects in R must follow 3 rules:

  1. No spaces
  2. No symbols other than . or _
  3. Names must begin with a letter
## Order of operations
25+7/4 #26.75
(25+7)/4 #8
## AND vs OR
5 > 3 && 3 > 4 #FALSE
5 > 3 || 3 > 4 #TRUE

Variable Types in R

All objects in R have a variable type. The most common are:

  • numeric: the object is a number
  • character: the object contains letters
  • logical: the object is either TRUE or FALSE
  • NA: the object is blank (categorized as logical, but may be part of a character or numeric vector)

We can use the class() function on an object to check its variable type.

class(2000) #numeric
class("apple") #character
class(NA) #logical