R Demo

data("anscombe")
View(anscombe)
plot(y1 ~ x1, data = anscombe)
lmfit <- lm(y1 ~ x1, data = anscombe)
lmfit
## 
## Call:
## lm(formula = y1 ~ x1, data = anscombe)
## 
## Coefficients:
## (Intercept)           x1  
##      3.0001       0.5001
abline(lmfit, col="red")

predict(lmfit, data.frame(x1 = 16))
##        1 
## 11.00155

R 數學運算

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
3 + 2 * 8 + 5
## [1] 24
(3 + 2) * 8 + 5
## [1] 45
a <- 3
b = 2
a + b
## [1] 5
numer <- 17.8
char  <- 'Hello World'
logic <- TRUE

class(numer)
## [1] "numeric"
card_length <- 3
card_width  <- "5 inches"
class(card_length)
## [1] "numeric"
class(card_width)
## [1] "character"
# card_length * card_width

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

向量

height_vec <- c(180,169,173)
name_vec   <- c("Brian", "Toby", "Sherry")
vec        <- c(1, 2, 'hello')
vec
## [1] "1"     "2"     "hello"
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 + 1
## [1] 2 3 4 5
x + c(1,2)
## [1] 2 4 4 6
a <- 1
a <- c(1)

x + 1
## [1] 2 3 4 5
x + c(1)
## [1] 2 3 4 5
x + c(1,1,1,1)
## [1] 2 3 4 5
x + c(1,2)
## [1] 2 4 4 6
x + c(1,2,1,2)
## [1] 2 4 4 6
x + c(1,2,3)
## Warning in x + c(1, 2, 3): 較長的物件長度並非較短物件長度的倍數
## [1] 2 4 6 5
x + c(1,2,3,1)
## [1] 2 4 6 5
class(a)
## [1] "numeric"
class(x)
## [1] "numeric"
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
seq(1,20)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
help(seq)
## starting httpd help server ... done
?seq

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

sum(1,2,3)
## [1] 6
x <- c(1,2,3,4,5,NA)
sum(x)
## [1] NA
sum(x, na.rm=TRUE)
## [1] 15
height_vec <- c(180, 169, 173)
names(height_vec)
## NULL
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
##  Brian   Toby Sherry 
##    180    169    173
height_vec[1]
## Brian 
##   180
height_vec[3]
## Sherry 
##    173
height_vec[c(2,3)]
##   Toby Sherry 
##    169    173
2:3
## [1] 2 3
height_vec[2:3]
##   Toby Sherry 
##    169    173
height_vec > 175
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
height_vec < 175
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
height_vec == 175
##  Brian   Toby Sherry 
##  FALSE  FALSE  FALSE
height_vec != 175
##  Brian   Toby Sherry 
##   TRUE   TRUE   TRUE
height_vec[2:3]
##   Toby Sherry 
##    169    173
height_vec[c(FALSE,TRUE,TRUE)]
##   Toby Sherry 
##    169    173
height_vec < 175
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
height_vec[height_vec < 175]
##   Toby Sherry 
##    169    173
(height_vec >= 170) & (height_vec < 180) 
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
height_vec[(height_vec >= 170) & (height_vec < 180)] 
## Sherry 
##    173
height_vec[(height_vec >= 170) | (height_vec < 180)] 
##  Brian   Toby Sherry 
##    180    169    173
## Practice

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

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

names(bmi_vec) <- name_vec
bmi_vec[bmi_vec < 18.5 | bmi_vec >= 24]
##     Toby   Sherry 
## 30.46112 14.36734

矩陣

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

c(c(85,73),c(72,64), c(59,66))
## [1] 85 73 72 64 59 66
?matrix
mat <- matrix(c(kevin, marry , jerry), nrow = 3, byrow = TRUE)
mat
##      [,1] [,2]
## [1,]   85   73
## [2,]   72   64
## [3,]   59   66
colnames(mat) <- c('first', 'second')
mat
##      first second
## [1,]    85     73
## [2,]    72     64
## [3,]    59     66
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')))
mat
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
dim(mat)
## [1] 3 2
nrow(mat)
## [1] 3
ncol(mat)
## [1] 2
mat
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat[1,]
##  first second 
##     85     73
mat[,1]
## kevin marry jerry 
##    85    72    59
mat[c(2,3),]
##       first second
## marry    72     64
## jerry    59     66
mat[2:3,]
##       first second
## marry    72     64
## jerry    59     66
mat[c(FALSE, TRUE, TRUE),]
##       first second
## marry    72     64
## jerry    59     66
mat[2,1]
## [1] 72
mat2 <- rbind(mat, c(78,63))
rownames(mat2)
## [1] "kevin" "marry" "jerry" ""
rownames(mat2)[1]
## [1] "kevin"
rownames(mat2)[4] <- 'Sam'
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
## Sam      78     63
mat3 <- cbind(mat, c(82,77,70))
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)
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
m1 <- matrix(1:9, nrow = 9)
m1
##       [,1]
##  [1,]    1
##  [2,]    2
##  [3,]    3
##  [4,]    4
##  [5,]    5
##  [6,]    6
##  [7,]    7
##  [8,]    8
##  [9,]    9
m2 <- matrix(1:9, nrow = 1)
m2
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]    1    2    3    4    5    6    7    8    9
m1 %*% m2
##       [,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
mat <-matrix(c(kevin, marry, jerry), nrow=3, byrow=TRUE, dimnames=list(c('kevin', 'marry', 'jerry'),c('first', 'second')))

# method 1
mat[,1] * 0.4 + mat[,2] * 0.6
## kevin marry jerry 
##  77.8  67.2  63.2
# method 2
mat[, 'first'] * 0.4 + mat[,'second'] * 0.6
## kevin marry jerry 
##  77.8  67.2  63.2
# method 3
# 3 X 2  %*% 2 X 1 = 3 X 1
mat %*% matrix(c(0.4,0.6), nrow=2)
##       [,1]
## kevin 77.8
## marry 67.2
## jerry 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[2] > temperature[1]
## [1] FALSE
temperature[2] < temperature[1]
## [1] TRUE
temperature_category <- factor(temperature, order=TRUE, levels=c("Low", "Medium", "High"))

temperature_category[3] > temperature_category[1]
## [1] TRUE
temperature_category[4] > temperature_category[3]
## [1] FALSE
levels(temperature_category)
## [1] "Low"    "Medium" "High"
levels(temperature_category) <- c('L', 'M', 'H')
temperature_category
## [1] L H H M L M
## Levels: L < M < H

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)

mat <- matrix(c(days, temp, rain), nrow = 5)

#sum(mat[,2])

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
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                  
##  wed:1   3rd Qu.:24.3                  
##          Max.   :25.0
#data()
data(iris)
View(iris)

class(iris)
## [1] "data.frame"
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 ...
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
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

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 , 6)
##     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
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, -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
head(iris$Sepal.Length)
## [1] 5.1 4.9 4.7 4.6 5.0 5.4
iris[iris$Species == 'setosa' & iris$Sepal.Length >= 5.2, ]
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 6           5.4         3.9          1.7         0.4  setosa
## 11          5.4         3.7          1.5         0.2  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
## 19          5.7         3.8          1.7         0.3  setosa
## 21          5.4         3.4          1.7         0.2  setosa
## 28          5.2         3.5          1.5         0.2  setosa
## 29          5.2         3.4          1.4         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
## 37          5.5         3.5          1.3         0.2  setosa
## 49          5.3         3.7          1.5         0.2  setosa
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_subset <- head(iris)

iris_subset
##   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
iris_subset[c(1,5,6),]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris_subset[c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE),]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris_subset$Sepal.Length >= 5
## [1]  TRUE FALSE FALSE FALSE  TRUE  TRUE
iris_subset[iris_subset$Sepal.Length >= 5,]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
which(iris_subset$Sepal.Length >= 5)
## [1] 1 5 6
iris_subset[which(iris_subset$Sepal.Length >= 5),]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
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(2,7,1,5,8,9,3)
sort(a)
## [1] 1 2 3 5 7 8 9
order(a)
## [1] 3 1 7 4 2 5 6
iris_subset[c(6,5,4,3,2,1), ]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 6          5.4         3.9          1.7         0.4  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 1          5.1         3.5          1.4         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
## Read 2330.TW csv
library(readr)
tw2330 <- read_csv("https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv", col_types = cols(High = col_number(),Open = col_number(), Close = col_number(), `Adj Close` = col_number(), Volume = col_number(),Low = col_number(), Date = col_date(format = "%Y-%m-%d")))
## `curl` package not installed, falling back to using `url()`
## Warning in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 84 parsing failures.
## row # A tibble: 5 x 5 col     row col       expected actual file                                     expected   <int> <chr>     <chr>    <chr>  <chr>                                    actual 1   423 Open      a number null   'https://raw.githubusercontent.com/ywch~ file 2   423 High      a number null   'https://raw.githubusercontent.com/ywch~ row 3   423 Low       a number null   'https://raw.githubusercontent.com/ywch~ col 4   423 Close     a number null   'https://raw.githubusercontent.com/ywch~ expected 5   423 Adj Close a number null   'https://raw.githubusercontent.com/ywch~
## ... ................. ... .......................................................................... ........ .......................................................................... ...... .......................................................................... .... .......................................................................... ... .......................................................................... ... .......................................................................... ........ ..........................................................................
## See problems(...) for more details.
View(tw2330)

str(tw2330)
## Classes 'tbl_df', 'tbl' and 'data.frame':    1232 obs. of  7 variables:
##  $ Date     : Date, format: "2013-02-25" "2013-02-26" ...
##  $ Open     : num  106 104 102 104 104 ...
##  $ High     : num  107 105 104 105 104 ...
##  $ Low      : num  105 102 101 102 102 ...
##  $ Close    : num  105 104 104 105 102 ...
##  $ Adj Close: num  90.2 88.9 89.7 90.2 87.6 ...
##  $ Volume   : num  35913000 38440000 80851000 31408000 50033000 ...
##  - attr(*, "problems")=Classes 'tbl_df', 'tbl' and 'data.frame': 84 obs. of  5 variables:
##   ..$ row     : int  423 423 423 423 423 423 428 428 428 428 ...
##   ..$ col     : chr  "Open" "High" "Low" "Close" ...
##   ..$ expected: chr  "a number" "a number" "a number" "a number" ...
##   ..$ actual  : chr  "null" "null" "null" "null" ...
##   ..$ file    : chr  "'https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv'" "'https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv'" "'https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv'" "'https://raw.githubusercontent.com/ywchiu/rtibame/master/Data/2330.TW.csv'" ...
##  - attr(*, "spec")=List of 2
##   ..$ cols   :List of 7
##   .. ..$ Date     :List of 1
##   .. .. ..$ format: chr "%Y-%m-%d"
##   .. .. ..- attr(*, "class")= chr  "collector_date" "collector"
##   .. ..$ Open     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   .. ..$ High     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   .. ..$ Low      : list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   .. ..$ Close    : list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   .. ..$ Adj Close: list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   .. ..$ Volume   : list()
##   .. .. ..- attr(*, "class")= chr  "collector_number" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr  "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
summary(tw2330)
##       Date                 Open            High            Low       
##  Min.   :2013-02-25   Min.   : 94.4   Min.   : 94.9   Min.   : 92.9  
##  1st Qu.:2014-05-27   1st Qu.:121.0   1st Qu.:122.0   1st Qu.:120.0  
##  Median :2015-08-24   Median :142.0   Median :143.0   Median :140.5  
##  Mean   :2015-08-24   Mean   :152.3   Mean   :153.4   Mean   :151.1  
##  3rd Qu.:2016-11-18   3rd Qu.:183.9   3rd Qu.:185.0   3rd Qu.:183.0  
##  Max.   :2018-02-23   Max.   :263.0   Max.   :266.0   Max.   :262.5  
##                       NA's   :14      NA's   :14      NA's   :14     
##      Close         Adj Close          Volume         
##  Min.   : 94.4   Min.   : 83.33   Min.   :        0  
##  1st Qu.:121.0   1st Qu.:107.69   1st Qu.: 22099500  
##  Median :142.0   Median :130.37   Median : 29911500  
##  Mean   :152.4   Mean   :142.97   Mean   : 33021623  
##  3rd Qu.:184.0   3rd Qu.:178.06   3rd Qu.: 39984500  
##  Max.   :266.0   Max.   :266.00   Max.   :157081000  
##  NA's   :14      NA's   :14       NA's   :14
head(tw2330[order(tw2330$Close, decreasing = TRUE),])
## # A tibble: 6 x 7
##   Date        Open  High   Low Close `Adj Close`   Volume
##   <date>     <dbl> <dbl> <dbl> <dbl>       <dbl>    <dbl>
## 1 2018-01-23   262   266   262   266         266 33527444
## 2 2018-01-22   258   262   257   262         262 43400509
## 3 2018-02-01   258   261   257   260         260 30559457
## 4 2018-02-02   259   260   255   260         260 25707560
## 5 2018-01-29   259   262   255   258         258 30071234
## 6 2018-01-24   263   263   256   258         258 41874813
head(tw2330[order(tw2330$Close, decreasing = FALSE),])
## # A tibble: 6 x 7
##   Date        Open  High   Low Close `Adj Close`   Volume
##   <date>     <dbl> <dbl> <dbl> <dbl>       <dbl>    <dbl>
## 1 2013-08-20  95.2  95.4  94.4  94.4        83.3 41635000
## 2 2013-08-22  94.4  94.9  92.9  94.5        83.4 60185000
## 3 2013-08-19  95.5  96.1  95.3  95.5        84.3 49081000
## 4 2013-08-27  96.0  96.8  95.6  95.6        84.4 22435000
## 5 2013-08-12  96.4  96.7  95.6  96.0        84.7 33389000
## 6 2013-08-15  96.4  96.4  95.3  96.0        84.7 42647000
plot(tw2330$Date, tw2330$Close, type= 'l')

tw2330_after_2017 <- tw2330[tw2330$Date >= '2017-01-01', c('Date','Close')]

summary(tw2330_after_2017)
##       Date                Close      
##  Min.   :2017-01-03   Min.   :179.5  
##  1st Qu.:2017-04-21   1st Qu.:193.4  
##  Median :2017-07-31   Median :214.0  
##  Mean   :2017-07-30   Mean   :214.4  
##  3rd Qu.:2017-11-08   3rd Qu.:232.5  
##  Max.   :2018-02-23   Max.   :266.0  
##                       NA's   :2
tapply(iris$Sepal.Length, iris$Species, sum)
##     setosa versicolor  virginica 
##      250.3      296.8      329.4

Lists

phone <-list(thing="iphoneX" , height=5.65, width=2.79 )
phone
## $thing
## [1] "iphoneX"
## 
## $height
## [1] 5.65
## 
## $width
## [1] 2.79
phone$height
## [1] 5.65
phone$thing
## [1] "iphoneX"
student <-list(name="Toby", score =c(87,57,72))
student$score
## [1] 87 57 72
student$score[2]
## [1] 57
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
li[[1]]
## [1]  3  5 12
li[[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

流程控制

x <- 3

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

length(x)
## [1] 5
x[1]
## [1] "sunny"
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"
for(weather in x){
  print(weather)
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
s   <- 0
cnt <- 0 
while (cnt <= 100){
  s   <- s + cnt
  cnt <- cnt + 1 
}
s
## [1] 5050
url<-'https://tw.appledaily.com/new/realtime/'
?paste
paste(url, 'hi')
## [1] "https://tw.appledaily.com/new/realtime/ hi"
paste(url, 'hi', sep = '' )
## [1] "https://tw.appledaily.com/new/realtime/hi"
paste(url, 'hi', sep = '/' )
## [1] "https://tw.appledaily.com/new/realtime//hi"
paste0(url,'hi')
## [1] "https://tw.appledaily.com/new/realtime/hi"
for(i in 1:10){
  #print(paste(url, i))
  #print(paste(url, i, sep = ''))
  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"
lapply(1:10, function(e) paste0(url,e) )
## [[1]]
## [1] "https://tw.appledaily.com/new/realtime/1"
## 
## [[2]]
## [1] "https://tw.appledaily.com/new/realtime/2"
## 
## [[3]]
## [1] "https://tw.appledaily.com/new/realtime/3"
## 
## [[4]]
## [1] "https://tw.appledaily.com/new/realtime/4"
## 
## [[5]]
## [1] "https://tw.appledaily.com/new/realtime/5"
## 
## [[6]]
## [1] "https://tw.appledaily.com/new/realtime/6"
## 
## [[7]]
## [1] "https://tw.appledaily.com/new/realtime/7"
## 
## [[8]]
## [1] "https://tw.appledaily.com/new/realtime/8"
## 
## [[9]]
## [1] "https://tw.appledaily.com/new/realtime/9"
## 
## [[10]]
## [1] "https://tw.appledaily.com/new/realtime/10"
# sapply -> simplified lapply
sapply(1:10, function(e) paste0(url,e) )
##  [1] "https://tw.appledaily.com/new/realtime/1" 
##  [2] "https://tw.appledaily.com/new/realtime/2" 
##  [3] "https://tw.appledaily.com/new/realtime/3" 
##  [4] "https://tw.appledaily.com/new/realtime/4" 
##  [5] "https://tw.appledaily.com/new/realtime/5" 
##  [6] "https://tw.appledaily.com/new/realtime/6" 
##  [7] "https://tw.appledaily.com/new/realtime/7" 
##  [8] "https://tw.appledaily.com/new/realtime/8" 
##  [9] "https://tw.appledaily.com/new/realtime/9" 
## [10] "https://tw.appledaily.com/new/realtime/10"

函數

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

addNum(3,5)
## [1] 8
addNum2 <- function(a , b){
  s <- a + b
  s
}

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


addNum3(3,5)
## [1] 8
addNum3(3)
## [1] 5
addNum4 <- function(a , b){
  s <- a*3 + b
  s
}

addNum4(5,2)
## [1] 17
addNum4(b=5,a=2)
## [1] 11
f <- function(a, b){
  a *2
}

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

#f2(3)

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)

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")

wordcount <- function(article){
  article.split <- strsplit(tolower(article), '[ |,|.|-|–]')
  article.vec   <- unlist(article.split)
  tb            <- table(article.vec)
  tb2           <- tb[! names(tb) %in% stopwords]
  tb2           <- tb2[nchar(names(tb2)) >= 2]
  sort(tb2, decreasing = TRUE)
}

wordcount(article)
## article.vec
##        america       american        country         people          great 
##             17             12              9              9              6 
##            new           make         nation      president      protected 
##              6              5              5              5              5 
##          right          world      americans          bring       citizens 
##              5              5              4              4              4 
##         dreams          heart           jobs          power          today 
##              4              4              4              4              4 
##         wealth        borders        capital      countries            day 
##              4              3              3              3              3 
##      factories       families        foreign            god     government 
##              3              3              3              3              3 
##           left            let       millions       nation’s          obama 
##              3              3              3              3              3 
##         united         action     allegiance        belongs          bless 
##              3              2              2              2              2 
##         breath       children           city        destiny          earth 
##              2              2              2              2              2 
##           face          fight         follow      forgotten       glorious 
##              2              2              2              2              2 
##           good           land           life           like           long 
##              2              2              2              2              2 
##         longer        loyalty            men         moment       mountain 
##              2              2              2              2              2 
##       movement       national        nations           oath          ocean 
##              2              2              2              2              2 
##            old           pain          party    politicians           safe 
##              2              2              2              2              2 
##           seek          share          small         states          stops 
##              2              2              2              2              2 
##       strength        success           talk          thank           time 
##              2              2              2              2              2 
##   transferring     washington            way          we’ve        winning 
##              2              2              2              2              2 
##          women        workers          years           “how           2017 
##              2              2              2              1              1 
##           20th         accept administration        affairs            aid 
##              1              1              1              1              1 
##       airports      alliances       allowing       almighty      america's 
##              1              1              1              1              1 
##         armies        arrives      assembled      beautiful        bedrock 
##              1              1              1              1              1 
##        benefit          bible            big         bigger          birth 
##              1              1              1              1              1 
##          black          bleed          blood           body           born 
##              1              1              1              1              1 
##          borne        bridges          brown          build           bush 
##              1              1              1              1              1 
##            buy           came        carnage          carry         carter 
##              1              1              1              1              1 
##           cash      celebrate     celebrated    celebration         center 
##              1              1              1              1              1 
##       ceremony      challenge     challenges        changes          chief 
##              1              1              1              1              1 
##          child        cities;      civilized          class        clinton 
##              1              1              1              1              1 
##         closed           come      companies    complaining     completely 
##              1              1              1              1              1 
##     confidence       confront     constantly     controlled       controls 
##              1              1              1              1              1 
##    conviction:           cost        courage         course        creator 
##              1              1              1              1              1 
##          crime        crucial         debate        decades          decay 
##              1              1              1              1              1 
##       decision         decree         defend       defended         define 
##              1              1              1              1              1 
##        demands      depletion       deprived     destroying      determine 
##              1              1              1              1              1 
##        detroit            did      different  disagreements    disappeared 
##              1              1              1              1              1 
##        disease      disrepair      divisions          doing        dollars 
##              1              1              1              1              1 
##          dream        dreams;          drugs      education         effort 
##              1              1              1              1              1 
##       energies    enforcement          enjoy       enriched         entire 
##              1              1              1              1              1 
##      eradicate  establishment        example         exists        exists: 
##              1              1              1              1              1 
##        expense           fail         fallen            far           fear 
##              1              1              1              1              1 
##         fellow        finally           flag     flourished          flush 
##              1              1              1              1              1 
##        forever        forget:           form        forward           free 
##              1              1              1              1              1 
##       freedoms     friendship         future          gangs         gather 
##              1              1              1              1              1 
##       gathered         giving          god’s          going       goodness 
##              1              1              1              1              1 
##       goodwill         govern       gracious       grateful          group 
##              1              1              1              1              1 
##          guide           hall          hands      hardships        harness 
##              1              1              1              1              1 
##           heal           hear          heard       highways           hire 
##              1              1              1              1              1 
##       historic           home          homes       honestly          hopes 
##              1              1              1              1              1 
##        horizon           hour        ignored    immigration    importantly 
##              1              1              1              1              1 
##         impose     industries       industry      industry; infrastructure 
##              1              1              1              1              1 
##        infused          inner      interests        islamic        issuing 
##              1              1              1              1              1 
##           it’s        january            job         joined           just 
##              1              1              1              1              1 
##        justice     knowledge;          labor           lady      landscape 
##              1              1              1              1              1 
##          large            law           lead         leaves           lift 
##              1              1              1              1              1 
##          likes      listening         little           live          lives 
##              1              1              1              1              1 
##         living           look        looking           love    magnificent 
##              1              1              1              1              1 
##         making          match        matters        meaning         merely 
##              1              1              1              1              1 
##       michelle         middle       military      military;     millennium 
##              1              1              1              1              1 
##          minds       miseries        moment:        mothers      mysteries 
##              1              1              1              1              1 
##        nation;           near       nebraska  neighborhoods          night 
##              1              1              1              1              1 
##         office           ones           open         openly        orderly 
##              1              1              1              1              1 
##       overseas           own;           past     patriotism       patriots 
##              1              1              1              1              1 
##       peaceful         plains       pleasant       politics      potential 
##              1              1              1              1              1 
##        poverty      prejudice          pride       products        promise 
##              1              1              1              1              1 
##        prosper      prospered     prosperity        protect     protection 
##              1              1              1              1              1 
##          proud         public         pursue        radical       railways 
##              1              1              1              1              1 
##        ravages          ready        reality         reaped     reasonable 
##              1              1              1              1              1 
##        rebuild     rebuilding            red     rediscover  redistributed 
##              1              1              1              1              1 
##       refusing      reinforce       remember     remembered        restore 
##              1              1              1              1              1 
##        rewards           rich      righteous         ripped          roads 
##              1              1              1              1              1 
##         robbed        roberts           room         rulers         rules: 
##              1              1              1              1              1 
##     rusted-out            sad         salute      scattered        schools 
##              1              1              1              1              1 
##           seen          serve          shine         shores      shuttered 
##              1              1              1              1              1 
##         sights         simple            sky       soldiers     solidarity 
##              1              1              1              1              1 
##          souls          space          speak        special          spent 
##              1              1              1              1              1 
##         spirit         sprawl          stand          start       starting 
##              1              1              1              1              1 
##       stealing          steps           stir         stolen       striving 
##              1              1              1              1              1 
##         strong     struggling       students     subsidized          taxes 
##              1              1              1              1              1 
##   technologies           tell          tells           tens      terrorism 
##              1              1              1              1              1 
##          think        thought         thrive        today’s     tombstones 
##              1              1              1              1              1 
##       tomorrow          total        totally          trade       transfer 
##              1              1              1              1              1 
##     transition        trapped      trillions       triumphs      triumphs; 
##              1              1              1              1              1 
##          truly        tunnels     understand  understanding          unite 
##              1              1              1              1              1 
##          unity         unlock     unrealized    unstoppable          urban 
##              1              1              1              1              1 
##      victories     victories;         vision          voice           want 
##              1              1              1              1              1 
##       watching          we've        wealthy        welfare          white 
##              1              1              1              1              1 
##      windswept         wisdom      wonderful         words:           work 
##              1              1              1              1              1 
##         world:            yes          young 
##              1              1              1
## install wordcloud2
library(wordcloud2)
wordcloud2(wordcount(article), shape = 'star')
## Read CNN
#f <-file('cnn.txt')
#article2 <-readLines(f)
#close(f)
#wordcloud2(wordcount(article2), shape = 'star')

## unlist
a <- list(c(1,2,3), c(7,8))
a
## [[1]]
## [1] 1 2 3
## 
## [[2]]
## [1] 7 8
unlist(a)
## [1] 1 2 3 7 8
## table
a <- c(2,3,1,1,2,3,3,2,3)
table(a)
## a
## 1 2 3 
## 2 3 4
## tolower
tolower('APPLE')
## [1] "apple"
## nchar
nchar('123')
## [1] 3