R Demo

data("anscombe")
View(anscombe)
fit <- lm(y1 ~ x1, data = anscombe)
plot(y1 ~ x1, data = anscombe)
abline(fit, col = 'red')

數學運算

3 + 8
## [1] 11
3 - 8
## [1] -5
3 * 8
## [1] 24
3 / 8
## [1] 0.375
2 ^ 10
## [1] 1024
11 %% 2
## [1] 1
a <- 3
b = 2
c <- a+b


numer <- 17.8
class(numer)
## [1] "numeric"
char <- 'hello world'
class(char)
## [1] "character"
logic <- TRUE
class(logic)
## [1] "logical"
a <- 3
class(a)
## [1] "numeric"
a <- "hello world"
class(a)
## [1] "character"
card_length <- 3
card_width <- '5 inches'
#card_length * card_width

card_width <- 5
card_length * card_width
## [1] 15
RRP <- 35.99
Exchange <- 31.74
NTD <- RRP * Exchange
NTD
## [1] 1142.323

向量

name1 <- 'david'
name2 <- 'sherry'
name3 <- 'brian'

c()
## NULL
height_vec <- c(180,169,173)
name_vec   <- c('Brian','Toby','Sherry')

height_vec
## [1] 180 169 173
height_vec[1]
## [1] 180
height_vec[2]
## [1] 169
height_vec[3]
## [1] 173
height_vec[c(1,3)]
## [1] 180 173
h_vec <- c(180,169,173, 'Joe')
h_vec
## [1] "180" "169" "173" "Joe"
class(h_vec)
## [1] "character"
x <- c(1,2,3,4)
y <- c(2,3,4,5)

x + y
## [1] 3 5 7 9
x - y
## [1] -1 -1 -1 -1
x * y
## [1]  2  6 12 20
x / y
## [1] 0.5000000 0.6666667 0.7500000 0.8000000
x + 3
## [1] 4 5 6 7
x + c(3)
## [1] 4 5 6 7
x + c(3,3,3,3)
## [1] 4 5 6 7
a <- 3
a <- c(3)

x + c(1,2)
## [1] 2 4 4 6
x + c(1,2,1,2)
## [1] 2 4 4 6
a <- c(1,2,3)
a <- c(1,2,3,4,5,6,7,8,9)
a <- 1:20
a
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
y <- seq(1,20)
y
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
y <- seq(from = 1,to = 20)

a <- c(4,5,6,7,8)
seq_along(a)
## [1] 1 2 3 4 5
help(seq)
## starting httpd help server ... done
seq(0, 1, length.out = 11)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
seq(1, 9, by = 2)
## [1] 1 3 5 7 9
seq(1, 9, length.out = 3)
## [1] 1 5 9
help(seq)
?seq

seq(1,20,2)
##  [1]  1  3  5  7  9 11 13 15 17 19
seq(1,3.5,by=0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5
seq(1,10, length =2)
## [1]  1 10
seq(1,10, length.out =2)
## [1]  1 10
seq(1,10, len =2)
## [1]  1 10
x <- c(1,2,3,5,7)
sum(x)
## [1] 18
sum(3,5)
## [1] 8
sum(3,5,7)
## [1] 15
?sum

x <- c(1,2,3,5,7, NA)
sum(x)
## [1] NA
sum(x, na.rm=TRUE)
## [1] 18
height_vec <- c(180,169,173)
height_vec
## [1] 180 169 173
names(height_vec) <- c('Brian', 'Toby', 'Sherry')
height_vec
##  Brian   Toby Sherry 
##    180    169    173
height_vec > 175
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
height_vec[height_vec > 175]
## Brian 
##   180
height_vec < 175
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
height_vec[height_vec < 175]
##   Toby Sherry 
##    169    173
height_vec == 180
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
height_vec[height_vec == 180]
## Brian 
##   180
height_vec != 180
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
height_vec[height_vec != 180]
##   Toby Sherry 
##    169    173
height_vec > 170 & height_vec < 180
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
height_vec >= 180 | height_vec < 170
##  Brian   Toby Sherry 
##   TRUE   TRUE  FALSE
height_vec > 175
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
which(height_vec > 175)
## Brian 
##     1
height_vec[which(height_vec > 175)]
## Brian 
##   180
height_vec[c(1)]
## Brian 
##   180

計算BMI

height_vec <- c(180,169,173)
weight_vec <- c(73,87,43)
names_vec  <- c('Brian', 'Toby', 'Sherry')

bmi_vec <- weight_vec / (height_vec / 100) ^ 2

bmi_vec[1]
## [1] 22.53086
names(bmi_vec) <- names_vec

bmi_vec[1]
##    Brian 
## 22.53086
bmi_vec['Brian']
##    Brian 
## 22.53086
bmi_vec[c('Brian')]
##    Brian 
## 22.53086
bmi_vec < 18.5 
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
bmi_vec >= 24 
##  Brian   Toby Sherry 
##  FALSE   TRUE  FALSE
(bmi_vec < 18.5) | (bmi_vec >= 24) 
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
bmi_vec[(bmi_vec < 18.5) | (bmi_vec >= 24) ]
##     Toby   Sherry 
## 30.46112 14.36734
?names
names(bmi_vec[(bmi_vec < 18.5) | (bmi_vec >= 24) ])
## [1] "Toby"   "Sherry"

矩陣

kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)


c(kevin, marry, jerry)
## [1] 85 73 72 64 59 66
mat <- matrix(
  c(kevin, marry, jerry), 
  nrow =3, 
  byrow=TRUE)


matrix(1:9, nrow=3)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
matrix(1:9, nrow=3, byrow=TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
mat <- matrix(
  c(kevin, marry, jerry), 
  nrow =3, 
  byrow=TRUE)

colnames(mat) <- c('first', 'second')
rownames(mat) <- c('kevin', 'marry', 'jerry')
mat
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat <- matrix(
  c(kevin, marry, jerry), 
  nrow=3, 
  byrow=TRUE,
  dimnames=list(
    c('kevin', 'marry', 'jerry')
   ,c('first', 'second')))

dim(mat)
## [1] 3 2
nrow(mat)
## [1] 3
ncol(mat)
## [1] 2
mat[1,]
##  first second 
##     85     73
mat[,1]
## kevin marry jerry 
##    85    72    59
mat[2:3,]
##       first second
## marry    72     64
## jerry    59     66
mat[,1:2]
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat[c('kevin', 'marry'),'first']
## kevin marry 
##    85    72
mat[c('kevin', 'marry'),c('first', 'second')]
##       first second
## kevin    85     73
## marry    72     64
mat[c('kevin', 'marry'),c(1,2)]
##       first second
## kevin    85     73
## marry    72     64
mat[c('kevin', 'marry'),c(TRUE,FALSE)]
## kevin marry 
##    85    72
mat2 <- rbind(mat, c(78,63))
rownames(mat2)
## [1] "kevin" "marry" "jerry" ""
rownames(mat2)[4] <- 'sam'
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
## sam      78     63
mat
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat3 <- cbind(mat,c(82,77,70))
mat3
##       first second   
## kevin    85     73 82
## marry    72     64 77
## jerry    59     66 70
colnames(mat3)
## [1] "first"  "second" ""
colnames(mat3)[3] <- 'third'
mat3
##       first second third
## kevin    85     73    82
## marry    72     64    77
## jerry    59     66    70
m1 <- matrix(1:4, byrow=TRUE, nrow=2)
m2 <- matrix(5:8, byrow=TRUE, nrow=2)

m2
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
m1 + m2
##      [,1] [,2]
## [1,]    6    8
## [2,]   10   12
m1 - m2
##      [,1] [,2]
## [1,]   -4   -4
## [2,]   -4   -4
m1 * m2
##      [,1] [,2]
## [1,]    5   12
## [2,]   21   32
m1 / m2
##           [,1]      [,2]
## [1,] 0.2000000 0.3333333
## [2,] 0.4285714 0.5000000
rowSums(mat)
## kevin marry jerry 
##   158   136   125
colSums(mat)
##  first second 
##    216    203
m1 %*% m2
##      [,1] [,2]
## [1,]   19   22
## [2,]   43   50
# 9 X 9
#   9 X 1 %*% 1 X 9 => 9 X 9
matrix(1:9, nrow = 9) %*% matrix(1:9, nrow = 1)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    2    3    4    5    6    7    8    9
##  [2,]    2    4    6    8   10   12   14   16   18
##  [3,]    3    6    9   12   15   18   21   24   27
##  [4,]    4    8   12   16   20   24   28   32   36
##  [5,]    5   10   15   20   25   30   35   40   45
##  [6,]    6   12   18   24   30   36   42   48   54
##  [7,]    7   14   21   28   35   42   49   56   63
##  [8,]    8   16   24   32   40   48   56   64   72
##  [9,]    9   18   27   36   45   54   63   72   81
kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)
mat <- matrix(c(kevin, marry, jerry), nrow=3, byrow= TRUE)


# method 1
mat[,1] * 0.4 + mat[,2] * 0.6
## [1] 77.8 67.2 63.2
# method 2
# 3 X 2 %*% 2 X 1 = 3 X 1
mat %*% matrix(c(0.4,0.6), nrow= 2)
##      [,1]
## [1,] 77.8
## [2,] 67.2
## [3,] 63.2

階層

weather <- c("sunny","rainy"
, "cloudy", "rainy", "cloudy")
class(weather)
## [1] "character"
weather_category <- factor(weather)
weather_category
## [1] sunny  rainy  cloudy rainy  cloudy
## Levels: cloudy rainy sunny
levels(weather_category)
## [1] "cloudy" "rainy"  "sunny"
temperature <- c("Low", "High", "High", "Medium", "Low", "Medium")

temperature_category <- factor(temperature, order = TRUE, levels=c("Low", "Medium", "High"))
temperature_category[1]
## [1] Low
## Levels: Low < Medium < High
temperature_category[2]
## [1] High
## Levels: Low < Medium < High
temperature_category[2] > temperature_category[1]
## [1] TRUE
temperature_category[4]
## [1] Medium
## Levels: Low < Medium < High
temperature_category[3]
## [1] High
## Levels: Low < Medium < High
temperature_category[4] > temperature_category[3]
## [1] FALSE
levels(temperature_category)
## [1] "Low"    "Medium" "High"

Data Frame

height <- c(180,169,173)
height
## [1] 180 169 173
class(height)
## [1] "numeric"
height <- c(height, 165 )
height
## [1] 180 169 173 165
class(height)
## [1] "numeric"
height <- c(height, 'hello' )
height
## [1] "180"   "169"   "173"   "165"   "hello"
class(height)
## [1] "character"
days <- c('mon','tue','wed','thu','fri')
temp <- c(22.2,21,23,24.3,25)
rain <- c(TRUE, TRUE, FALSE, FALSE, TRUE)

m <- matrix(c(days, temp, rain), nrow = 5)
class(m)
## [1] "matrix"
m
##      [,1]  [,2]   [,3]   
## [1,] "mon" "22.2" "TRUE" 
## [2,] "tue" "21"   "TRUE" 
## [3,] "wed" "23"   "FALSE"
## [4,] "thu" "24.3" "FALSE"
## [5,] "fri" "25"   "TRUE"
mean(temp)
## [1] 23.1
#mean(m[,2])


df <- data.frame(days,temp,rain)
class(df)
## [1] "data.frame"
View(df)
str(df)
## 'data.frame':    5 obs. of  3 variables:
##  $ days: Factor w/ 5 levels "fri","mon","thu",..: 2 4 5 3 1
##  $ temp: num  22.2 21 23 24.3 25
##  $ rain: logi  TRUE TRUE FALSE FALSE TRUE
summary(df)
##   days        temp         rain        
##  fri:1   Min.   :21.0   Mode :logical  
##  mon:1   1st Qu.:22.2   FALSE:2        
##  thu:1   Median :23.0   TRUE :3        
##  tue:1   Mean   :23.1                  
##  wed:1   3rd Qu.:24.3                  
##          Max.   :25.0
data()
data(iris)
View(iris)
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
class(iris)
## [1] "data.frame"
?head
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
head(iris, 3)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
tail(iris)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
tail(iris,3)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
iris[1:3,]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
head(iris[,1:2])
##   Sepal.Length Sepal.Width
## 1          5.1         3.5
## 2          4.9         3.0
## 3          4.7         3.2
## 4          4.6         3.1
## 5          5.0         3.6
## 6          5.4         3.9
iris[1:3,1]
## [1] 5.1 4.9 4.7
iris[1:3,'Sepal.Length']
## [1] 5.1 4.9 4.7
head(iris$Species)
## [1] setosa setosa setosa setosa setosa setosa
## Levels: setosa versicolor virginica
#iris$Species == 'setosa'
#which(iris$Species == 'setosa')
head(iris[iris$Species == 'setosa', c('Sepal.Length', 'Sepal.Width')])
##   Sepal.Length Sepal.Width
## 1          5.1         3.5
## 2          4.9         3.0
## 3          4.7         3.2
## 4          4.6         3.1
## 5          5.0         3.6
## 6          5.4         3.9
head(iris[iris$Species == 'setosa', c(1,2)])
##   Sepal.Length Sepal.Width
## 1          5.1         3.5
## 2          4.9         3.0
## 3          4.7         3.2
## 4          4.6         3.1
## 5          5.0         3.6
## 6          5.4         3.9
head(iris[iris$Species == 'setosa', 1:4])
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4
head(iris[iris$Species == 'setosa', -5])
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1          5.1         3.5          1.4         0.2
## 2          4.9         3.0          1.4         0.2
## 3          4.7         3.2          1.3         0.2
## 4          4.6         3.1          1.5         0.2
## 5          5.0         3.6          1.4         0.2
## 6          5.4         3.9          1.7         0.4
head(sort(iris$Sepal.Length))
## [1] 4.3 4.4 4.4 4.4 4.5 4.6
head(sort(iris$Sepal.Length, decreasing = TRUE))
## [1] 7.9 7.7 7.7 7.7 7.7 7.6
a <- c(70,50,60,80,90)
sort(a)
## [1] 50 60 70 80 90
order(a)
## [1] 2 3 1 4 5
order(a, decreasing = TRUE)
## [1] 5 4 1 3 2
a[order(a)]
## [1] 50 60 70 80 90
head(iris[order(iris$Sepal.Length), ])
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 14          4.3         3.0          1.1         0.1  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 39          4.4         3.0          1.3         0.2  setosa
## 43          4.4         3.2          1.3         0.2  setosa
## 42          4.5         2.3          1.3         0.3  setosa
## 4           4.6         3.1          1.5         0.2  setosa
head(iris[order(iris$Sepal.Length, decreasing = TRUE), ])
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 132          7.9         3.8          6.4         2.0 virginica
## 118          7.7         3.8          6.7         2.2 virginica
## 119          7.7         2.6          6.9         2.3 virginica
## 123          7.7         2.8          6.7         2.0 virginica
## 136          7.7         3.0          6.1         2.3 virginica
## 106          7.6         3.0          6.6         2.1 virginica
tw2330 <- read.csv('https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv', na.strings = 'null')

class(tw2330)
## [1] "data.frame"
str(tw2330)
## 'data.frame':    1231 obs. of  7 variables:
##  $ Date     : Factor w/ 1231 levels "2013-08-12","2013-08-13",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ Open     : num  96.4 96 96 96.4 95.4 ...
##  $ High     : num  96.7 97.4 97.3 96.4 97.5 ...
##  $ Low      : num  95.6 96 95.8 95.3 95.2 ...
##  $ Close    : num  96 97.2 96.8 96 96.5 ...
##  $ Adj.Close: num  81.8 82.8 82.4 81.8 82.2 ...
##  $ Volume   : int  33389000 34361000 41618000 42647000 37509000 49081000 41635000 60185000 44276000 18663000 ...
summary(tw2330)
##          Date           Open            High            Low       
##  2013-08-12:   1   Min.   : 93.0   Min.   : 94.9   Min.   : 92.9  
##  2013-08-13:   1   1st Qu.:127.5   1st Qu.:128.5   1st Qu.:125.5  
##  2013-08-14:   1   Median :149.0   Median :149.5   Median :147.5  
##  2013-08-15:   1   Mean   :164.3   Mean   :165.4   Mean   :163.0  
##  2013-08-16:   1   3rd Qu.:204.0   3rd Qu.:205.0   3rd Qu.:203.0  
##  2013-08-19:   1   Max.   :263.0   Max.   :266.0   Max.   :262.5  
##  (Other)   :1225   NA's   :6       NA's   :6       NA's   :6      
##      Close         Adj.Close         Volume         
##  Min.   : 94.4   Min.   : 80.4   Min.   :        0  
##  1st Qu.:127.5   1st Qu.:112.8   1st Qu.: 21840151  
##  Median :148.5   Median :131.9   Median : 29134000  
##  Mean   :164.3   Mean   :151.0   Mean   : 32469195  
##  3rd Qu.:204.0   3rd Qu.:190.5   3rd Qu.: 39253000  
##  Max.   :266.0   Max.   :256.6   Max.   :157081000  
##  NA's   :6       NA's   :6       NA's   :6
head(tw2330[order(tw2330$Close),])
##          Date Open High  Low Close Adj.Close   Volume
## 7  2013-08-20 95.2 95.4 94.4  94.4  80.39552 41635000
## 8  2013-08-22 93.0 94.9 92.9  94.5  80.48069 60185000
## 6  2013-08-19 95.5 96.1 95.3  95.5  81.33234 49081000
## 11 2013-08-27 96.0 96.8 95.6  95.6  81.41750 22435000
## 1  2013-08-12 96.4 96.7 95.6  96.0  81.75816 33389000
## 4  2013-08-15 96.4 96.4 95.3  96.0  81.75816 42647000
head(tw2330[order(tw2330$Close, decreasing = TRUE),])
##            Date  Open  High   Low Close Adj.Close   Volume
## 1100 2018-01-23 262.5 266.0 262.5 266.0  256.6461 33527444
## 1099 2018-01-22 257.5 262.0 257.0 261.5  252.3044 43400509
## 1107 2018-02-01 257.5 261.0 257.0 259.5  250.3747 30559457
## 1108 2018-02-02 259.0 260.0 255.0 259.5  250.3747 25707560
## 1128 2018-03-13 255.5 259.0 255.0 259.0  249.8923 33756883
## 1104 2018-01-29 259.0 261.5 255.0 258.5  249.4099 30071234
plot(Close ~ Date, data = tw2330)

清單

list()
## list()
phone <- list(thing="iphone X" , height=5.65, width=2.79 )

phone$thing
## [1] "iphone X"
phone$height
## [1] 5.65
student <- list(name="Toby", score = c(87,57,72))
student$score
## [1] 87 57 72
(student$score ^ (1/2) ) * 10
## [1] 93.27379 75.49834 84.85281
li <- list(c(3,5,12), c(2,4,5,8,10))
li
## [[1]]
## [1]  3  5 12
## 
## [[2]]
## [1]  2  4  5  8 10
li[[1]]
## [1]  3  5 12
li[[2]]
## [1]  2  4  5  8 10
student <- list(name="Toby", score = c(87,57,72))
student$name
## [1] "Toby"
student[[1]]
## [1] "Toby"
li <- list(c(3,5,12), c(2,4,5,8,10))
li
## [[1]]
## [1]  3  5 12
## 
## [[2]]
## [1]  2  4  5  8 10
sum(li[[1]])
## [1] 20
sum(li[[2]])
## [1] 29
lapply(li, sum)
## [[1]]
## [1] 20
## 
## [[2]]
## [1] 29
lapply(li, mean)
## [[1]]
## [1] 6.666667
## 
## [[2]]
## [1] 5.8
lapply(li, min)
## [[1]]
## [1] 3
## 
## [[2]]
## [1] 2
lapply(li, max)
## [[1]]
## [1] 12
## 
## [[2]]
## [1] 10

流程控制

x <- 2

if(x > 3){
  print('x > 3')
}else{
  print('x <= 3')
}
## [1] "x <= 3"
x <-3

if(x > 3){
  print('x > 3')
}else if (x == 3){
  print('x == 3')
}else{
  print('x < 3')
}
## [1] "x == 3"
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
for(qoo in 1:10){
  print(qoo)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## Bad Method
s <- 0
for(i in 1:100){
  s <- s + i
}
s
## [1] 5050
## Good Method
sum(1:100)
## [1] 5050
x <- c("sunny","rainy"
, "cloudy", "rainy", "cloudy")

for(weather in x){
  print(weather)
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
length(x)
## [1] 5
for(i in 1:length(x)) {
  print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
seq_along(x)
## [1] 1 2 3 4 5
for(i in seq_along(x)) {
print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
cnt <- 0
s   <- 0
while(cnt <= 100){
  s   <- s + cnt
  cnt <- cnt + 1
}
s
## [1] 5050
cnt
## [1] 101
url <- 'https://tw.appledaily.com/new/realtime/'
paste(url, 1)
## [1] "https://tw.appledaily.com/new/realtime/ 1"
?paste

paste(url, 1, sep='')
## [1] "https://tw.appledaily.com/new/realtime/1"
paste0(url, 1)
## [1] "https://tw.appledaily.com/new/realtime/1"
for(i in 1:10){
  print(paste(url,i, sep=''))
}
## [1] "https://tw.appledaily.com/new/realtime/1"
## [1] "https://tw.appledaily.com/new/realtime/2"
## [1] "https://tw.appledaily.com/new/realtime/3"
## [1] "https://tw.appledaily.com/new/realtime/4"
## [1] "https://tw.appledaily.com/new/realtime/5"
## [1] "https://tw.appledaily.com/new/realtime/6"
## [1] "https://tw.appledaily.com/new/realtime/7"
## [1] "https://tw.appledaily.com/new/realtime/8"
## [1] "https://tw.appledaily.com/new/realtime/9"
## [1] "https://tw.appledaily.com/new/realtime/10"
for(i in 1:10){
  print(paste0(url,i))
}
## [1] "https://tw.appledaily.com/new/realtime/1"
## [1] "https://tw.appledaily.com/new/realtime/2"
## [1] "https://tw.appledaily.com/new/realtime/3"
## [1] "https://tw.appledaily.com/new/realtime/4"
## [1] "https://tw.appledaily.com/new/realtime/5"
## [1] "https://tw.appledaily.com/new/realtime/6"
## [1] "https://tw.appledaily.com/new/realtime/7"
## [1] "https://tw.appledaily.com/new/realtime/8"
## [1] "https://tw.appledaily.com/new/realtime/9"
## [1] "https://tw.appledaily.com/new/realtime/10"

函數

addNum <- function(a,b){
  a + b
}

addNum(a = 3,b = 2)
## [1] 5
addNum(3,2)
## [1] 5
#addNum(3)
addNum <- function(a,b=2){
  a + b
}

addNum(3,2)
## [1] 5
addNum(3)
## [1] 5
addNum <- function(a,b=2){
  return(a + b)
}
addNum(3,2)
## [1] 5
f <- function(a, b) {
    a * 2
}

f(3)
## [1] 6
f <- function(a, b) {
    a + b
}

#f(3)

f <- function(a, b) {
    a * 3 + b
}

f(2,3)
## [1] 9
f(b=2,a=3)
## [1] 11
f <- function(abb, bcc) {
    abb * 3 + bcc
}
f(b=2,a=3)
## [1] 11

撰寫函式計算文章詞頻

stopwords <- c("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount",  "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as",  "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the")

## read articles
f <- file('https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/trump.txt')
article <- readLines(f)
## Warning in readLines(f): 於 'https://raw.githubusercontent.com/ywchiu/
## rtibame/master/Data/trump.txt' 找到不完整的最後一列
close(f)


## split the words by delimiter
?strsplit()

article.split <- strsplit(tolower(article), split = ' ')
class(article.split)
## [1] "list"
## unlist the list
article.vec <- unlist(article.split)

## using table to count words
tb    <- table(article.vec)

## filter out stopwords and words with charater length below 2
sort(tb[(! names(tb) %in% stopwords) & (nchar(names(tb)) >= 2)], decreasing = TRUE)[1:10]
## article.vec
##  american   america    again.   country     great       new    people 
##        11        10         8         6         6         6         6 
##      make president     right 
##         5         5         5
## Using table to count
a <- c(3,3,2,1,2,2,2,3)
?table
table(a)
## a
## 1 2 3 
## 1 4 3
## Generate a word count function
wordcount <- function(article){
  article.split <- strsplit(tolower(article), split = ' |,|\\.')
  
  article.vec <- unlist(article.split)
  tb    <- table(article.vec)
  sort(tb[(! names(tb) %in% stopwords) & (nchar(names(tb)) >= 2)], decreasing = TRUE)  
}


#wordcount(article)

#install.packages('wordcloud2')
library(wordcloud2)
?wordcloud2
wordcloud2(wordcount(article), shape = 'star')

strsplit

a = 'hi, my name is David.'
?strsplit
strsplit(a, split = ' |,|\\.')
## [[1]]
## [1] "hi"    ""      "my"    "name"  "is"    "David"
#  ' |,|\\.' , split the string by " " OR "," OR "."