Skip to content

Basic syntax

Arithmetic operators

#| eval: false

x + y   # Addition
x - y   # Subtraction
x * y   # Multiplication
x / y   # Division
x ^ y   # Raise to a power
x ** y  # Raise to a power (old style /still not deprecated/ translated to ^ in the parser.)
x %% y  # Modulus - remainder after the division
x %/% y # Integer Division

x, y: numeric or complex vectors or objects which can be coerced to such, or other objects for which methods have been written.

For documentation write ?Arithmetic in the R console:

#| collapse: true
?Arithmetic
help("Arithmetic")

Using example we can run the examples in the end of the documentation:

#| collapse: true
example("Arithmetic")

Using apropos we can look for similar function names:

#| collapse: true
apropos("sq")

Using find we can find from which package is the function.

#| collapse: true
find("sqrt")

We can only search functions from the already loaded packages.

#| collapse: true
find("simple.lm")
library(UsingR)
find("simple.lm")

Asignement operators

For documentation:

#| collapse: true
?assignOps

There are 3 different assignment operators:

#| collapse: true
x = 5
y <- 5  # Recommended
5 -> z
x; y; z # Prints

Differences between them:

  1. They have different operator precedence (look at `?Syntax`).
  2. `=` has two meanings:
    • operator: assignment operator
    • syntax token: named argument passing in a function call
#| collapse: true
x = 20
mean(x = 3); x
mean(x <- 3); x # Not a good idea

Syntax

  • R is case sensitive

    #| collapse: true
    #| error: true
    A <- 5
    a
    

  • R is (dynamic language) not typified

    #| collapse: true
    a <- 5
    a <- 5.4
    a <- "string"
    

  • Valid names consist of letters, numbers and the dot or underline character.

Functions

  • R can have default arguments and the arguments can be matched by position or by name.

Printing values

You can print an object just by typing its name, because R is wrapping that object name within the print command, so the following lines of code are identical:

#| collapse: true
a
print(a)

R uses the idea of generic functions, so print function looks for the attribute class of the object and the class type shows print how to generate the output.

print gives some options for formatting the output

  • removing the quotes from the output
#| collapse: true
print(a)
print(a, quote = FALSE)
  • determine how many digits from the output to be shown
#| collapse: true
a <- 3145.429357453; a
print(a, digits = 10)

Also we can redirect the output to a file using sink and then return it back to the console

#| eval: false
sink("myoutput.txt")

Working directory

getwd returns the absolute path to the current working directory.

#| collapse: true
getwd()

The working directory tells R where to look for files and where to create files. So the file that you have just created in the previous example myoutput.txt will be created in this directory.

If you want to change the working directory you can use setwd function

#| eval: false
setwd("<path-to-directory>")

Objects

Everything in R is an object.

ls prints all names of the objects in the global environment

ls()

rm removes objects from the current environment

rm(x, y, z)
ls()

You can also remove all the objects from the current environment using

#| results: asis
rm(list = ls())
ls()
character(0)

Additional notes

  • Comments start with #

  • For execution on Linux (in terminal):

        Rscript main.R