■ 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`

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')`

🌻 2 Notations of Assignment : `=`

和
`<-`

```
= c(0.1, 0.2, 0.3, 0.4)
Prob <- c(120, 100, -50, -60)
Value * Value Prob
```

`[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`

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

Some functions produce vectors.

```
=c(500,20,75,400)
valsum(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

■ Place cursor on the function name and press F1
to see the online help

■ Arguments can be given either

■ 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

```
= 10000
x mean(log(sqrt(x), base=10))
```

`[1] 2`

The `%>%`

would make it easier to
apply a series of functions

`sqrt(x) %>% log(10) %>% mean`

`[1] 2`