數學運算

# 數字相加
3 + 8
## [1] 11
# 數字相減
3 - 8
## [1] -5
# 數字相乘
5 * 5
## [1] 25
# 數字相除
11 / 2
## [1] 5.5
# 指數
2^10
## [1] 1024
# 取餘數
11%%2
## [1] 1

設定變數

# 指定變數
a <- 3
a
## [1] 3
# 變數相加
b <- 5
c <- a + b
c
## [1] 8

基礎資料型態

# 數值型態
numer <- 17.8
# 字串型態
char <- "hello world"
# 布林邏輯
logic <- TRUE
class(char)
## [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

使用向量存放多個變數的資料

height_vec <- c(180,169,173)
height_vec[1]
## [1] 180
height_vec[3]
## [1] 173
name_vec <- c("Brian", "Toby", "Sherry")
name_vec[2]
## [1] "Toby"

向量的運算

x <- c(1,2,3,7) 
y <- c(2,3,5,1)

x + y
## [1] 3 5 8 8
x - y
## [1] -1 -1 -2  6
x * y
## [1]  2  6 15  7
x / y
## [1] 0.5000000 0.6666667 0.6000000 7.0000000

自動產生向量

x <- 1:20
x
##  [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
?seq
## starting httpd help server ...
##  done
seq()
## [1] 1
seq(1,10,by=2)
## [1] 1 3 5 7 9

seq

#使用? 或help 去觀看seq 的用法
?seq
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

將向量作加總

scores <- c(53, 62, 78, 92, 70)
sum(scores)
## [1] 355
mean(scores)
## [1] 71
summary(scores)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      53      62      70      71      78      92
scores2 <- c(53, 62, 78, 92, 70, NA)
sum(scores2)
## [1] NA
?sum
sum(scores2, na.rm=TRUE)
## [1] 355

指定名稱

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
name_vec <- c("Brian", "Toby", "Sherry")
names(height_vec) <- name_vec

height_vec > 175
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
height_vec[height_vec > 175]
## Brian 
##   180
height_vec < 180 & height_vec > 170
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
height_vec >= 180 | height_vec < 170 
##  Brian   Toby Sherry 
##   TRUE   TRUE  FALSE
height_vec[height_vec >= 180 | height_vec < 170]
## Brian  Toby 
##   180   169

使用向量計算BMI

height_vec <- c(180,169,173)
weight_vec <- c(73,87,43)
bmi_vec <- weight_vec / (height_vec / 100) ^ 2
names(bmi_vec) <- c('Brian', 'Toby', 'Sherry')
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

產生陣列

1:9
## [1] 1 2 3 4 5 6 7 8 9
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
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)
colnames(mat) <- c('first', 'second')
rownames(mat) <- c('kevin', 'marry', 'jerry')

mat2 <- matrix(c(kevin, marry, jerry), nrow=3, byrow=TRUE, 
dimnames=list(c('kevin', 'marry', 'jerry'),c('first', 'second')))
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
dim(mat2)
## [1] 3 2
nrow(mat2)
## [1] 3
ncol(mat2)
## [1] 2

依欄或列取矩陣資料

#取第一列
mat2[1,] 
##  first second 
##     85     73
#取第一行
mat2[,1]
## kevin marry jerry 
##    85    72    59
#取第二、三列
mat2[2:3,]
##       first second
## marry    72     64
## jerry    59     66
#取第二列第一行的元素
mat2[2,1]
## [1] 72

新增列與行

mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
#新增學生資料
mat3 <- rbind(mat2, c(78,63))
rownames(mat3)[nrow(mat3)] <- 'sam'
mat3
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
## sam      78     63
#新增考試分數
mat4 <- cbind(mat2,c(82,77,70))
colnames(mat4)[ncol(mat4)] <- 'third'
mat4
##       first second third
## kevin    85     73    82
## marry    72     64    77
## jerry    59     66    70
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
rowSums(mat2)
## kevin marry jerry 
##   158   136   125
colSums(mat2)
##  first second 
##    216    203

矩陣運算

m1 <- matrix(1:4, byrow=TRUE, nrow=2)
m2 <- matrix(5:8, byrow=TRUE, nrow=2)

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
m1 %*% m2
##      [,1] [,2]
## [1,]   19   22
## [2,]   43   50

使用矩陣計算考試成績

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

sumavg <- mat[,1] *0.4 + mat[,2] * 0.6
mat <- cbind(mat, sumavg)
mat
##       first second sumavg
## kevin    85     73   77.8
## marry    72     64   67.2
## jerry    59     66   63.2
kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)
mat <- matrix(c(kevin, marry, jerry), nrow=3, byrow= TRUE)
colnames(mat) <- c('first', 'second')
rownames(mat) <- c('kevin', 'marry', 'jerry')
mat %*% c(0.4,0.6)
##       [,1]
## kevin 77.8
## marry 67.2
## jerry 63.2
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

將資料轉換為類別資料(Factor)

Weather <- c("sunny","rainy", "cloudy", "rainy", "cloudy")
class(Weather)
## [1] "character"
weather_category <- factor(Weather)
class(weather_category)
## [1] "factor"
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] Low    High   High   Medium Low    Medium
## Levels: Low < Medium < High
temperature_category[3] > temperature_category[1]
## [1] TRUE
temperature_category[4] > temperature_category[3]
## [1] FALSE

建立Data Frame

days <- c('mon','tue','wed','thu','fri')
temp <- c(22.2,21,23,24.3,25)
rain <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
df <- data.frame(days, temp, rain)
df
##   days temp  rain
## 1  mon 22.2  TRUE
## 2  tue 21.0  TRUE
## 3  wed 23.0 FALSE
## 4  thu 24.3 FALSE
## 5  fri 25.0  TRUE
View(df)
class(df)
## [1] "data.frame"
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   NA's :0        
##  wed:1   3rd Qu.:24.3                  
##          Max.   :25.0

使用R 內建的資料集

data()
data(iris)
class(iris)
## [1] "data.frame"
View(iris)
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,10)
##    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
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  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, 10)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 141          6.7         3.1          5.6         2.4 virginica
## 142          6.9         3.1          5.1         2.3 virginica
## 143          5.8         2.7          5.1         1.9 virginica
## 144          6.8         3.2          5.9         2.3 virginica
## 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
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
iris[1:3, 1 ]
## [1] 5.1 4.9 4.7
iris[1:3, "Sepal.Length" ]
## [1] 5.1 4.9 4.7
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
## 7            4.6         3.4
## 8            5.0         3.4
## 9            4.4         2.9
## 10           4.9         3.1
## 11           5.4         3.7
## 12           4.8         3.4
## 13           4.8         3.0
## 14           4.3         3.0
## 15           5.8         4.0
## 16           5.7         4.4
## 17           5.4         3.9
## 18           5.1         3.5
## 19           5.7         3.8
## 20           5.1         3.8
## 21           5.4         3.4
## 22           5.1         3.7
## 23           4.6         3.6
## 24           5.1         3.3
## 25           4.8         3.4
## 26           5.0         3.0
## 27           5.0         3.4
## 28           5.2         3.5
## 29           5.2         3.4
## 30           4.7         3.2
## 31           4.8         3.1
## 32           5.4         3.4
## 33           5.2         4.1
## 34           5.5         4.2
## 35           4.9         3.1
## 36           5.0         3.2
## 37           5.5         3.5
## 38           4.9         3.6
## 39           4.4         3.0
## 40           5.1         3.4
## 41           5.0         3.5
## 42           4.5         2.3
## 43           4.4         3.2
## 44           5.0         3.5
## 45           5.1         3.8
## 46           4.8         3.0
## 47           5.1         3.8
## 48           4.6         3.2
## 49           5.3         3.7
## 50           5.0         3.3
## 51           7.0         3.2
## 52           6.4         3.2
## 53           6.9         3.1
## 54           5.5         2.3
## 55           6.5         2.8
## 56           5.7         2.8
## 57           6.3         3.3
## 58           4.9         2.4
## 59           6.6         2.9
## 60           5.2         2.7
## 61           5.0         2.0
## 62           5.9         3.0
## 63           6.0         2.2
## 64           6.1         2.9
## 65           5.6         2.9
## 66           6.7         3.1
## 67           5.6         3.0
## 68           5.8         2.7
## 69           6.2         2.2
## 70           5.6         2.5
## 71           5.9         3.2
## 72           6.1         2.8
## 73           6.3         2.5
## 74           6.1         2.8
## 75           6.4         2.9
## 76           6.6         3.0
## 77           6.8         2.8
## 78           6.7         3.0
## 79           6.0         2.9
## 80           5.7         2.6
## 81           5.5         2.4
## 82           5.5         2.4
## 83           5.8         2.7
## 84           6.0         2.7
## 85           5.4         3.0
## 86           6.0         3.4
## 87           6.7         3.1
## 88           6.3         2.3
## 89           5.6         3.0
## 90           5.5         2.5
## 91           5.5         2.6
## 92           6.1         3.0
## 93           5.8         2.6
## 94           5.0         2.3
## 95           5.6         2.7
## 96           5.7         3.0
## 97           5.7         2.9
## 98           6.2         2.9
## 99           5.1         2.5
## 100          5.7         2.8
## 101          6.3         3.3
## 102          5.8         2.7
## 103          7.1         3.0
## 104          6.3         2.9
## 105          6.5         3.0
## 106          7.6         3.0
## 107          4.9         2.5
## 108          7.3         2.9
## 109          6.7         2.5
## 110          7.2         3.6
## 111          6.5         3.2
## 112          6.4         2.7
## 113          6.8         3.0
## 114          5.7         2.5
## 115          5.8         2.8
## 116          6.4         3.2
## 117          6.5         3.0
## 118          7.7         3.8
## 119          7.7         2.6
## 120          6.0         2.2
## 121          6.9         3.2
## 122          5.6         2.8
## 123          7.7         2.8
## 124          6.3         2.7
## 125          6.7         3.3
## 126          7.2         3.2
## 127          6.2         2.8
## 128          6.1         3.0
## 129          6.4         2.8
## 130          7.2         3.0
## 131          7.4         2.8
## 132          7.9         3.8
## 133          6.4         2.8
## 134          6.3         2.8
## 135          6.1         2.6
## 136          7.7         3.0
## 137          6.3         3.4
## 138          6.4         3.1
## 139          6.0         3.0
## 140          6.9         3.1
## 141          6.7         3.1
## 142          6.9         3.1
## 143          5.8         2.7
## 144          6.8         3.2
## 145          6.7         3.3
## 146          6.7         3.0
## 147          6.3         2.5
## 148          6.5         3.0
## 149          6.2         3.4
## 150          5.9         3.0
iris$"Sepal.Length"
##   [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4
##  [18] 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5
##  [35] 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0
##  [52] 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8
##  [69] 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4
##  [86] 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8
## [103] 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7
## [120] 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7
## [137] 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
iris[1:5, 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
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
## 7           4.6         3.4
## 8           5.0         3.4
## 9           4.4         2.9
## 10          4.9         3.1
## 11          5.4         3.7
## 12          4.8         3.4
## 13          4.8         3.0
## 14          4.3         3.0
## 15          5.8         4.0
## 16          5.7         4.4
## 17          5.4         3.9
## 18          5.1         3.5
## 19          5.7         3.8
## 20          5.1         3.8
## 21          5.4         3.4
## 22          5.1         3.7
## 23          4.6         3.6
## 24          5.1         3.3
## 25          4.8         3.4
## 26          5.0         3.0
## 27          5.0         3.4
## 28          5.2         3.5
## 29          5.2         3.4
## 30          4.7         3.2
## 31          4.8         3.1
## 32          5.4         3.4
## 33          5.2         4.1
## 34          5.5         4.2
## 35          4.9         3.1
## 36          5.0         3.2
## 37          5.5         3.5
## 38          4.9         3.6
## 39          4.4         3.0
## 40          5.1         3.4
## 41          5.0         3.5
## 42          4.5         2.3
## 43          4.4         3.2
## 44          5.0         3.5
## 45          5.1         3.8
## 46          4.8         3.0
## 47          5.1         3.8
## 48          4.6         3.2
## 49          5.3         3.7
## 50          5.0         3.3
which(iris$Species == 'setosa')
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
iris[which(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
## 7           4.6         3.4
## 8           5.0         3.4
## 9           4.4         2.9
## 10          4.9         3.1
## 11          5.4         3.7
## 12          4.8         3.4
## 13          4.8         3.0
## 14          4.3         3.0
## 15          5.8         4.0
## 16          5.7         4.4
## 17          5.4         3.9
## 18          5.1         3.5
## 19          5.7         3.8
## 20          5.1         3.8
## 21          5.4         3.4
## 22          5.1         3.7
## 23          4.6         3.6
## 24          5.1         3.3
## 25          4.8         3.4
## 26          5.0         3.0
## 27          5.0         3.4
## 28          5.2         3.5
## 29          5.2         3.4
## 30          4.7         3.2
## 31          4.8         3.1
## 32          5.4         3.4
## 33          5.2         4.1
## 34          5.5         4.2
## 35          4.9         3.1
## 36          5.0         3.2
## 37          5.5         3.5
## 38          4.9         3.6
## 39          4.4         3.0
## 40          5.1         3.4
## 41          5.0         3.5
## 42          4.5         2.3
## 43          4.4         3.2
## 44          5.0         3.5
## 45          5.1         3.8
## 46          4.8         3.0
## 47          5.1         3.8
## 48          4.6         3.2
## 49          5.3         3.7
## 50          5.0         3.3

資料排序

scores <- c(54,99,63,72,80)
sort(scores)
## [1] 54 63 72 80 99
order(scores)
## [1] 1 3 4 5 2
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
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 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
sort(iris$Sepal.Length, decreasing = TRUE)
##   [1] 7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 7.2 7.1 7.0 6.9 6.9 6.9 6.9
##  [18] 6.8 6.8 6.8 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.6 6.6 6.5 6.5 6.5 6.5
##  [35] 6.5 6.4 6.4 6.4 6.4 6.4 6.4 6.4 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3
##  [52] 6.2 6.2 6.2 6.2 6.1 6.1 6.1 6.1 6.1 6.1 6.0 6.0 6.0 6.0 6.0 6.0 5.9
##  [69] 5.9 5.9 5.8 5.8 5.8 5.8 5.8 5.8 5.8 5.7 5.7 5.7 5.7 5.7 5.7 5.7 5.7
##  [86] 5.6 5.6 5.6 5.6 5.6 5.6 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.4 5.4 5.4 5.4
## [103] 5.4 5.4 5.3 5.2 5.2 5.2 5.2 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.0
## [120] 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 4.9 4.9 4.9 4.9 4.9 4.9 4.8 4.8
## [137] 4.8 4.8 4.8 4.7 4.7 4.6 4.6 4.6 4.6 4.5 4.4 4.4 4.4 4.3
order(iris$Sepal.Length, decreasing = TRUE)
##   [1] 132 118 119 123 136 106 131 108 110 126 130 103  51  53 121 140 142
##  [18]  77 113 144  66  78  87 109 125 141 145 146  59  76  55 105 111 117
##  [35] 148  52  75 112 116 129 133 138  57  73  88 101 104 124 134 137 147
##  [52]  69  98 127 149  64  72  74  92 128 135  63  79  84  86 120 139  62
##  [69]  71 150  15  68  83  93 102 115 143  16  19  56  80  96  97 100 114
##  [86]  65  67  70  89  95 122  34  37  54  81  82  90  91   6  11  17  21
## [103]  32  85  49  28  29  33  60   1  18  20  22  24  40  45  47  99   5
## [120]   8  26  27  36  41  44  50  61  94   2  10  35  38  58 107  12  13
## [137]  25  31  46   3  30   4   7  23  48  42   9  39  43  14
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
## 131          7.4         2.8          6.1         1.9  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 51           7.0         3.2          4.7         1.4 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 121          6.9         3.2          5.7         2.3  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 77           6.8         2.8          4.8         1.4 versicolor
## 113          6.8         3.0          5.5         2.1  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 66           6.7         3.1          4.4         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 109          6.7         2.5          5.8         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 59           6.6         2.9          4.6         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 105          6.5         3.0          5.8         2.2  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 52           6.4         3.2          4.5         1.5 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 112          6.4         2.7          5.3         1.9  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 57           6.3         3.3          4.7         1.6 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 69           6.2         2.2          4.5         1.5 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 127          6.2         2.8          4.8         1.8  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 64           6.1         2.9          4.7         1.4 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 128          6.1         3.0          4.9         1.8  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 63           6.0         2.2          4.0         1.0 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 120          6.0         2.2          5.0         1.5  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 62           5.9         3.0          4.2         1.5 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 150          5.9         3.0          5.1         1.8  virginica
## 15           5.8         4.0          1.2         0.2     setosa
## 68           5.8         2.7          4.1         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 102          5.8         2.7          5.1         1.9  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 16           5.7         4.4          1.5         0.4     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 56           5.7         2.8          4.5         1.3 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 114          5.7         2.5          5.0         2.0  virginica
## 65           5.6         2.9          3.6         1.3 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 122          5.6         2.8          4.9         2.0  virginica
## 34           5.5         4.2          1.4         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 54           5.5         2.3          4.0         1.3 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 6            5.4         3.9          1.7         0.4     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 85           5.4         3.0          4.5         1.5 versicolor
## 49           5.3         3.7          1.5         0.2     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 60           5.2         2.7          3.9         1.4 versicolor
## 1            5.1         3.5          1.4         0.2     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 99           5.1         2.5          3.0         1.1 versicolor
## 5            5.0         3.6          1.4         0.2     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 61           5.0         2.0          3.5         1.0 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 2            4.9         3.0          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 58           4.9         2.4          3.3         1.0 versicolor
## 107          4.9         2.5          4.5         1.7  virginica
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 42           4.5         2.3          1.3         0.3     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
## 14           4.3         3.0          1.1         0.1     setosa

2330 分析

#download.file('http://chart.finance.yahoo.com/table.csv?s=2330.TW&a=0&b=4&c=2000&d=2&e=25&f=2017&g=d&ignore=.csv', '2330.csv')

library(readr)
tw2330 <- read_csv("2330.csv")
View(tw2330)
max(tw2330$Close)
min(tw2330$Close)
summary(tw2330)

tw2330[order(tw2330$Close, decreasing = TRUE),]

plot(tw2330$Date, tw2330$Close)

max(tw2330[tw2330$Date >= '2015-01-01', 'Close'])
min(tw2330[tw2330$Date >= '2015-01-01', 'Close'])


library(quantmod)
getSymbols('2330.JP')
chart_Series(`2330.TW`)

建立list

item <- list(thing= "hat", size = 8.25)
item
item$size

test <- list(name="Toby", score = c(87,57,72)) test$score
test$score[2]

li <- list(c(3,5,12), c(2,4,5,8,10))
li
li[[1]]
li[[1]][2]

lapply(li, sum)
lapply(li, mean)

IF…ELSE…

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

For 迴圈

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
for (i in 1:10){
  print(paste("hi",i))
}
## [1] "hi 1"
## [1] "hi 2"
## [1] "hi 3"
## [1] "hi 4"
## [1] "hi 5"
## [1] "hi 6"
## [1] "hi 7"
## [1] "hi 8"
## [1] "hi 9"
## [1] "hi 10"
s <- 0 
for(i in 1:100){
  s <- s + i
}
s
## [1] 5050
sum(1:100)
## [1] 5050
x <- c("sunny","rainy", "cloudy", "rainy", "cloudy")

for(i in 1:length(x)) {
 print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for(i in seq_along(x)) {
 print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for(letter in x) {
 print(letter)
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"

While 迴圈

s <- 0 
cnt <- 0 

while(cnt <= 100){
  s   <- s + cnt
  cnt <- cnt + 1
}
s
## [1] 5050

範例: 產生多筆頁面連結

url <- 'http://www.appledaily.com.tw/realtimenews/section/new/'


?paste
paste(url, 0)
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/ 0"
paste(url, 0, sep="^_^")
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/^_^0"
paste(url, 0, sep = '')
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/0"
paste0(url, 0)
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/0"
for (i in 1:10){
  print(paste0(url,i))
}
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/1"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/2"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/3"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/4"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/5"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/6"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/7"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/8"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/9"
## [1] "http://www.appledaily.com.tw/realtimenews/section/new/10"

Function

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

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

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

g(2)
## [1] 8
g(3,5)
## [1] 18
g(b=3,a=5)
## [1] 14
f = function(a, b) {
   a * 2
}
f(3)
## [1] 6
f = function(a, b) {
     print(a+ b)
}
#f(3)

範例: 撰寫函式計算文章詞頻

wordcount <- function(article){
    #a <- 'hello, my name is david, I like my name'
     table(strsplit(a, ' '))
}

trump = "Chief Justice Roberts, President Carter, President Clinton, President Bush, President Obama, fellow Americans, and people of the world: Thank you.
We, the citizens of America, are now joined in a great national effort to rebuild our country and to restore its promise for all of our people.
Together, we will determine the course of America and the world for years to come.
We will face challenges. We will confront hardships. But we will get the job done.
Every four years, we gather on these steps to carry out the orderly and peaceful transfer of power, and we are grateful to President Obama and First Lady Michelle Obama for their gracious aid throughout this transition. They have been magnificent.
Today's ceremony, however, has very special meaning. Because today we are not merely transferring power from one administration to another, or from one party to another -- but we are transferring power from Washington, D.C. and giving it back to you, the American People.
Words from the past: Inauguration speech library
For too long, a small group in our nation's Capital has reaped the rewards of government while the people have borne the cost. Washington flourished -- but the people did not share in its wealth. Politicians prospered -- but the jobs left, and the factories closed.
The establishment protected itself, but not the citizens of our country. Their victories have not been your victories; their triumphs have not been your triumphs; and while they celebrated in our nation's capital, there was little to celebrate for struggling families all across our land.
That all changes -- starting right here, and right now, because this moment is your moment: it belongs to you.
It belongs to everyone gathered here today and everyone watching all across America. This is your day. This is your celebration. And this, the United States of America, is your country.
What truly matters is not which party controls our government, but whether our government is controlled by the people. January 20th 2017, will be remembered as the day the people became the rulers of this nation again. The forgotten men and women of our country will be forgotten no longer.
Everyone is listening to you now.
You came by the tens of millions to become part of a historic movement the likes of which the world has never seen before. At the center of this movement is a crucial conviction: that a nation exists to serve its citizens.
Americans want great schools for their children, safe neighborhoods for their families, and good jobs for themselves. These are the just and reasonable demands of a righteous public.
But for too many of our citizens, a different reality exists: Mothers and children trapped in poverty in our inner cities; rusted-out factories scattered like tombstones across the landscape of our nation; an education system flush with cash, but which leaves our young and beautiful students deprived of knowledge; and the crime and gangs and drugs that have stolen too many lives and robbed our country of so much unrealized potential.
This American carnage stops right here and stops right now.
We are one nation -- and their pain is our pain. Their dreams are our dreams; and their success will be our success. We share one heart, one home, and one glorious destiny.
The oath of office I take today is an oath of allegiance to all Americans.
For many decades, we've enriched foreign industry at the expense of American industry; subsidized the armies of other countries while allowing for the very sad depletion of our military; we've defended other nation's borders while refusing to defend our own; and spent trillions of dollars overseas while America's infrastructure has fallen into disrepair and decay.
We've made other countries rich while the wealth, strength, and confidence of our country has disappeared over the horizon.
One by one, the factories shuttered and left our shores, with not even a thought about the millions upon millions of American workers left behind.
The wealth of our middle class has been ripped from their homes and then redistributed across the entire world.
But that is the past. And now we are looking only to the future. We assembled here today are issuing a new decree to be heard in every city, in every foreign capital, and in every hall of power.
From this day forward, a new vision will govern our land.
From this moment on, it's going to be America First.
Every decision on trade, on taxes, on immigration, on foreign affairs, will be made to benefit American workers and American families. We must protect our borders from the ravages of other countries making our products, stealing our companies, and destroying our jobs. Protection will lead to great prosperity and strength.
I will fight for you with every breath in my body -- and I will never, ever let you down.
America will start winning again, winning like never before.
We will bring back our jobs. We will bring back our borders. We will bring back our wealth. And we will bring back our dreams.
We will build new roads, and highways, and bridges, and airports, and tunnels, and railways all across our wonderful nation.
We will get our people off of welfare and back to work -- rebuilding our country with American hands and American labor.
We will follow two simple rules: Buy American and hire American.
We will seek friendship and goodwill with the nations of the world -- but we do so with the understanding that it is the right of all nations to put their own interests first.
We do not seek to impose our way of life on anyone, but rather to let it shine as an example for everyone to follow.
We will reinforce old alliances and form new ones -- and unite the civilized world against radical Islamic terrorism, which we will eradicate completely from the face of the Earth.
At the bedrock of our politics will be a total allegiance to the United States of America, and through our loyalty to our country, we will rediscover our loyalty to each other.
When you open your heart to patriotism, there is no room for prejudice. The Bible tells us, How good and pleasant it is when God's people live together in unity.
We must speak our minds openly, debate our disagreements honestly, but always pursue solidarity.
When America is united, America is totally unstoppable.
There should be no fear -- we are protected, and we will always be protected.
We will be protected by the great men and women of our military and law enforcement and, most importantly, we are protected by God.
Finally, we must think big and dream even bigger.
In America, we understand that a nation is only living as long as it is striving.
We will no longer accept politicians who are all talk and no action -- constantly complaining but never doing anything about it.
The time for empty talk is over. Now arrives the hour of action.
Do not let anyone tell you it cannot be done. No challenge can match the heart and fight and spirit of America.
We will not fail. Our country will thrive and prosper again.
We stand at the birth of a new millennium, ready to unlock the mysteries of space, to free the Earth from the miseries of disease, and to harness the energies, industries and technologies of tomorrow.
A new national pride will stir our souls, lift our sights, and heal our divisions.
It is time to remember that old wisdom our soldiers will never forget: that whether we are black or brown or white, we all bleed the same red blood of patriots, we all enjoy the same glorious freedoms, and we all salute the same great American Flag.
And whether a child is born in the urban sprawl of Detroit or the windswept plains of Nebraska, they look up at the same night sky, they fill their heart with the same dreams, and they are infused with the breath of life by the same almighty Creator.
So to all Americans, in every city near and far, small and large, from mountain to mountain, and from ocean to ocean, hear these words:
You will never be ignored again.
Your voice, your hopes, and your dreams will define our American destiny. And your courage and goodness and love will forever guide us along the way.
Together, We will make America strong again.
We will make wealthy again.
We will make America proud again.
We will make America safe again.
And yes, together, we will make America great again. Thank you. God bless you. And God bless America."



library(tm)
## Loading required package: NLP
wordcount <- function(article){
    #a <- 'hello, my name is david, I like my name'
    stopwords_regex = paste(stopwords('en'), collapse = '\\b|\\b')
    stopwords_regex = paste0('\\b', stopwords_regex, '\\b')
    documents = stringr::str_replace_all(article, stopwords_regex, '')
     table(strsplit(documents, ' '))
}





a <- wordcount(trump)
sort(a, decreasing = TRUE)[1:20]
## 
##              will       -- American  America        ,        .       We 
##      578       40       11       11        8        7        7        7 
##     back  country    great      new      one   people    .\nWe   across 
##        6        6        6        6        6        6        5        5 
##      And    every     make    never 
##        5        5        5        5