👉 R:主要運算符號
■ 剛開始的時候,R的首要功能就是做向量(vector
)運算
■ 大多數R的運算符號和功能函數都主要都是對向量做計算
■
數學運算符號和功能函數運算之後會產生數值向量
🌻 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 🌻 Test for equivalence (==
) is different from the
assignment operator (=
)
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
%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('Danny', 'Cindy')
🌻 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
🌻 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.)
=c(500,20,75,400) val
summary functions produce a single summaries/statistics
sum(val)
[1] 995
mean(val)
[1] 248.8
math functions produce vectors applies to every elements
log(val)
[1] 6.215 2.996 4.317 5.991
sqrt(val)
[1] 22.361 4.472 8.660 20.000
功能選項(arguments):In addition to the input vector, most R function take extra arguments for options
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
💡 功能選項(arguments):
■
為了彈性,多數的功能都有很多個選項
■
為了方便性,多數的功能選項都有預設值
■
將滑鼠的遊標放在功能名稱上,按下F1
鍵就可以看到功能的定義
■ 在功能定義中,每個選項都有一個名稱
■
假如果你根據功能定義之中各選項的次序來設定選項,就可以不用打選項名稱
(call by position
)
■
假如果你指定選項名稱,選項的次序就不必和根據功能定義中的選項次序一樣
(call by name
)
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