1. Vectors and Operators

👉 FYR: List of R Operators

1.1 Mathematical Operators

  ■ The preliminary objective of R is to simply vector computation
  ■ Mathematical Operators and most R build-in functions take vectors as input

🌻 Bi-Operant Math Operations act Element-wise

c(1, 2, 3, 4) * c(1, 10, 100, 1000)
[1]    1   20  300 4000

🌻 When the lengths of the vectors are different …

c(100, 200, 300, 400) / 10
[1] 10 20 30 40

The shorter vector are repeated silently

c(100, 200, 300, 400) / c(10, 20)
[1] 10 10 30 20

There is a warning when …

c(10,20,30,40,50,60,70,80) + c(1,2,3)
Warning in c(10, 20, 30, 40, 50, 60, 70, 80) + c(1, 2, 3): longer object length is not a
multiple of shorter object length
[1] 11 22 33 41 52 63 71 82



1.2 Logical Operators

Logical Operations do comparisons and produce logical vectors

They compare numerics, strings, factors, Date, …

c(0.1, 0.2, 0.3, 0.4) > c(0, 1, 2, 3)
[1]  TRUE FALSE FALSE FALSE

shorten vectors are also repeated

c(100, 200, 300, 400) > 250
[1] FALSE FALSE  TRUE  TRUE

❓ what happen if I do …

c(200, 300) > c(100, 200, 300, 400)


🌻 Test for equivalence (==) on character and factor vectors

c('Amy','Bob','Cindy','Danny') == 'Cindy'
[1] FALSE FALSE  TRUE FALSE

🌻 The Set Comparison Operator : %in%

c('Amy','Bob','Cindy','Danny') %in% c('Danny','Cindy')
[1] FALSE FALSE  TRUE  TRUE

The above is the same as …

c('Amy','Bob','Cindy','Danny') %in% c('Cindy','Danny')
[1] FALSE FALSE  TRUE  TRUE
  • sequence in the rhs of %in% is not important

but different from …

c('Amy','Bob','Cindy','Danny') == c('Danny','Cindy')
[1] FALSE FALSE FALSE FALSE
  • == works element wise. thus, the sequence in the rhs vector of is important

❓ What happen if I do …

c('Amy','Bob','Cindy','Danny') == c('Cindy','Danny')


1.3 The Assignment Operator

🌻 2 Notations of Assignment : =<-

Prob = c(0.1, 0.2, 0.3, 0.4)
Value <- c(120, 100, -50, -60)
Prob * Value
[1]  12  20 -15 -24
  • the 2 notations are identical

🌻 Assignment (=) is different from Test for Eq. (==)

c(0.1, 0.2, 0.3, 0.4) == (1:4)/10
[1] TRUE TRUE TRUE TRUE

❓ What happen if you do

c(0.1, 0.2, 0.3, 0.4) = (1:4)/10


2. Functions & Their Arguments

Most R function take vectors as input (usually the first argument.)
Some functions produce vectors.

val=c(500,20,75,400)
sum(val)
[1] 995

Some functions produce summary statistics

mean(val)
[1] 248.8

In addition to the input, most R function take many other arguments

log(val, base=10)
[1] 2.699 1.301 1.875 2.602

💡 Arguments of Functions:
  ■ To be convenient and flexible, most R functions have many arguments with defaults
  ■ Place cursor on the function name and press F1 to see the online help
  ■ Arguments can be given either by name or by position
  ■ Unnamed-arguments must be in their exact position
  ■ Named arguments can be placed in any order

help(log)

Default argument

log(1000)
[1] 6.908

Argument by position

log(1000, 10)
[1] 3

Argument by names

log(x=1000, base=10)
[1] 3

Argument by names in reverse order

log(base=10, x=1000)
[1] 3

Quite often we need to cascade several functions, for example

x = 10000
mean(log(sqrt(x), base=10))
[1] 2

The pipe operator %>% would make it easier to apply a series of functions

sqrt(x) %>% log(10) %>% mean
[1] 2