■ 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
[1] 1 20 300 4000
🌻 When the lengths of the vectors are different …
[1] 10 20 30 40
The shorter vector are repeated silently
[1] 10 10 30 20
There is a warning when …
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, …
[1] TRUE FALSE FALSE FALSE
shorten vectors are also repeated
[1] FALSE FALSE TRUE TRUE
❓ what happen if I do …
🌻 Test for equivalence (==
) on character and factor
vectors
[1] FALSE FALSE TRUE FALSE
🌻 The Set Comparison Operator : %in%
[1] FALSE FALSE TRUE TRUE
The above is the same as …
[1] FALSE FALSE TRUE TRUE
%in%
is not importantbut different from …
[1] FALSE FALSE FALSE FALSE
==
works element wise. thus, the sequence in the rhs
vector of is important❓ What happen if I do …
🌻 2 Notations of Assignment : =
和
<-
[1] 12 20 -15 -24
🌻 Assignment (=
) is different from Test for Eq.
(==
)
[1] TRUE TRUE TRUE TRUE
❓ What happen if you do
Most R function take vectors as input (usually the first
argument.)
Some functions produce vectors.
[1] 995
Some functions produce summary statistics
[1] 248.8
In addition to the input, most R function take many other arguments
[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
Default argument
[1] 6.908
Argument by position
[1] 3
Argument by names
[1] 3
Argument by names in reverse order
[1] 3
Quite often we need to cascade several functions, for example
[1] 2
The %>%
would make it easier to
apply a series of functions
[1] 2