Anscombe Regresssion

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

R Basic

a <- 3
b <- 2
a + b
## [1] 5
# 數字相加
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
b = 2

c <- a + b
c
## [1] 5
numer <- 17.8
char <- "Hello world"
logic <- TRUE
class(logic)
## [1] "logical"
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] 8
RRP <- 35.9
#rrp
Exchange <- 31.74
NTD <- RRP * Exchange
NTD
## [1] 1139.466

Vector

height <- 170
height2 <- 165
height3 <- 180

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

height_vec[1]
## [1] 180
name_vec[3]
## [1] "Sherry"
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
height_vec / 100
## [1] 1.80 1.69 1.73
height_vec / c(100)
## [1] 1.80 1.69 1.73
height_vec / c(100, 100, 100)
## [1] 1.80 1.69 1.73
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
y <- seq(from=1, to=20)

?seq
## starting httpd help server ...
##  done
help(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(1,3.5,0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5
seq(1,10, length.out = 3)
## [1]  1.0  5.5 10.0
x <- c(1,2,3,5,7)
sum(x)
## [1] 18
mean(x)
## [1] 3.6
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     2.0     3.0     3.6     5.0     7.0
?sum
y <- c(1,2,3, NA)
sum(y)
## [1] NA
sum(y, na.rm=TRUE)
## [1] 6
height_vec <- c(180, 169, 173)
name_vec   <- c("Brian", "Toby", 'Sherry')
height_vec
## [1] 180 169 173
names(height_vec) <- name_vec
height_vec
##  Brian   Toby Sherry 
##    180    169    173
height_vec > 170
##  Brian   Toby Sherry 
##   TRUE  FALSE   TRUE
height_vec < 170
##  Brian   Toby Sherry 
##  FALSE   TRUE  FALSE
height_vec != 180
##  Brian   Toby Sherry 
##  FALSE   TRUE   TRUE
height_vec[height_vec > 170]
##  Brian Sherry 
##    180    173
height_vec[height_vec > 160 & height_vec < 170]
## Toby 
##  169
height_vec[height_vec < 160 | height_vec > 170]
##  Brian Sherry 
##    180    173

Use Vector to calculate BMI

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

bmi_vec <- weight_vec / (height_vec / 100)^2
bmi_vec
## [1] 22.53086 30.46112 14.36734
names(bmi_vec) <- name_vec
#bmi_vec < 18.5 | bmi_vec >= 24
# get Toby's BMI
bmi_vec[2]
##     Toby 
## 30.46112
bmi_vec[c(FALSE, TRUE, FALSE)]
##     Toby 
## 30.46112
abnormal <- bmi_vec[bmi_vec < 18.5 | bmi_vec >= 24]
abnormal
##     Toby   Sherry 
## 30.46112 14.36734
names(abnormal)
## [1] "Toby"   "Sherry"
normal <- bmi_vec[bmi_vec >= 18.5 & bmi_vec < 24]
normal
##    Brian 
## 22.53086
names(normal)
## [1] "Brian"

Matrix

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')
mat
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
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  ,  1]
## [1] 72
#2:3
mat2[ 2:3  ,   ]
##       first second
## marry    72     64
## jerry    59     66
mat2[c(2,3),   ]
##       first second
## marry    72     64
## jerry    59     66
mat3 <- rbind(mat2,  c(78 , 63) )
rownames(mat3)
## [1] "kevin" "marry" "jerry" ""
rownames(mat3)[4] <- '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)[3] <- 'third'
mat4
##       first second third
## kevin    85     73    82
## marry    72     64    77
## jerry    59     66    70
rowSums(mat4)
## kevin marry jerry 
##   240   213   195
colSums(mat4)
##  first second  third 
##    216    203    229
m1 <- matrix(1:4, nrow = 2, byrow =TRUE)
m1
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
m2 <- matrix(5:8, nrow = 2, byrow =TRUE)
m2
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
m1 + m2 
##      [,1] [,2]
## [1,]    6    8
## [2,]   10   12
m1 - m2
##      [,1] [,2]
## [1,]   -4   -4
## [2,]   -4   -4
m1 * m2
##      [,1] [,2]
## [1,]    5   12
## [2,]   21   32
m1 / m2
##           [,1]      [,2]
## [1,] 0.2000000 0.3333333
## [2,] 0.4285714 0.5000000
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
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')

#Method 1
score <- mat[  , 1 ] * 0.4 + mat[ , 2] * 0.6
mat2 <- cbind(mat, score)
mat2
##       first second score
## kevin    85     73  77.8
## marry    72     64  67.2
## jerry    59     66  63.2
#Method 2
score2 <- mat %*% matrix(c(0.4, 0.6), nrow = 2)
score2
##       [,1]
## kevin 77.8
## marry 67.2
## jerry 63.2

Factor

Weather<- c('sunny', 'rainny', 'cloudy', 'rainny', 'cloudy')
class(Weather)
## [1] "character"
weather_category <- factor(Weather)
weather_category
## [1] sunny  rainny cloudy rainny cloudy
## Levels: cloudy rainny sunny
levels(weather_category)
## [1] "cloudy" "rainny" "sunny"
weather_category[1] > weather_category[2]
## Warning in Ops.factor(weather_category[1], weather_category[2]): '>' not
## meaningful for factors
## [1] NA
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
levels(temperature_category)
## [1] "Low"    "Medium" "High"

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)
matrix(c(days, temp, rain), nrow = 5)
##      [,1]  [,2]   [,3]   
## [1,] "mon" "22.2" "TRUE" 
## [2,] "tue" "21"   "TRUE" 
## [3,] "wed" "23"   "FALSE"
## [4,] "thu" "24.3" "FALSE"
## [5,] "fri" "25"   "TRUE"
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"
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
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
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 ...
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 ]
##   [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:3, 1]
## [1] 5.1 4.9 4.7
iris[1:3, 'Sepal.Width']
## [1] 3.5 3.0 3.2
iris[1:3, c('Sepal.Width', 'Sepal.Length') ]
##   Sepal.Width Sepal.Length
## 1         3.5          5.1
## 2         3.0          4.9
## 3         3.2          4.7
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
five.Sepal.iris <- iris[1:5, c("Sepal.Length", "Sepal.Width")]
five.Sepal.iris
##   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
setosa.data <- iris[iris$Species=="setosa",1:5]

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
a <- c(2,5,1,3,6,4)
sort(a)
## [1] 1 2 3 4 5 6
order(a)
## [1] 3 1 4 6 2 5
head(sort(iris$Sepal.Length))
## [1] 4.3 4.4 4.4 4.4 4.5 4.6
head(iris[order(iris$Sepal.Length) ,   ])
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 14          4.3         3.0          1.1         0.1  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 39          4.4         3.0          1.3         0.2  setosa
## 43          4.4         3.2          1.3         0.2  setosa
## 42          4.5         2.3          1.3         0.3  setosa
## 4           4.6         3.1          1.5         0.2  setosa
head(iris[order(iris$Sepal.Length, decreasing = TRUE) ,   ])
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 132          7.9         3.8          6.4         2.0 virginica
## 118          7.7         3.8          6.7         2.2 virginica
## 119          7.7         2.6          6.9         2.3 virginica
## 123          7.7         2.8          6.7         2.0 virginica
## 136          7.7         3.0          6.1         2.3 virginica
## 106          7.6         3.0          6.6         2.1 virginica
head(iris[iris$Species == 'setosa' & iris$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
## 8           5.0         3.4          1.5         0.2  setosa
## 11          5.4         3.7          1.5         0.2  setosa
## 15          5.8         4.0          1.2         0.2  setosa
setosa.data <- iris[iris$Species == 'setosa', ] 
head(setosa.data[setosa.data$Sepal.Length >=5,], 3)
##   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

Stock Analysis

#install.packages("quantmod")
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols('2330.TW', src='yahoo')
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## Warning: 2330.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "2330.TW"
chartSeries(`2330.TW`)

library(readr)
X2330_TW <- read_csv("D:/OS DATA/Desktop/2330.TW.csv")
## Parsed with column specification:
## cols(
##   Date = col_date(format = ""),
##   Open = col_character(),
##   High = col_character(),
##   Low = col_character(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_character()
## )
## Warning in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 58 parsing failures.
## row # A tibble: 5 x 5 col     row       col expected actual                             file expected   <int>     <chr>    <chr>  <chr>                            <chr> actual 1  4163     Close a double   null 'D:/OS DATA/Desktop/2330.TW.csv' file 2  4163 Adj Close a double   null 'D:/OS DATA/Desktop/2330.TW.csv' row 3  4169     Close a double   null 'D:/OS DATA/Desktop/2330.TW.csv' col 4  4169 Adj Close a double   null 'D:/OS DATA/Desktop/2330.TW.csv' expected 5  4170     Close a double   null 'D:/OS DATA/Desktop/2330.TW.csv'
## ... ................. ... .................................................................. ........ .................................................................. ...... .................................................................. .... .................................................................. ... .................................................................. ... .................................................................. ........ ..................................................................
## See problems(...) for more details.
View(X2330_TW)

head(X2330_TW)
## # A tibble: 6 x 7
##         Date      Open      High       Low    Close `Adj Close`
##       <date>     <chr>     <chr>     <chr>    <dbl>       <dbl>
## 1 2000-01-04 69.649002 69.649002 68.475197 182.8103    55.17915
## 2 2000-01-05 69.649002 71.214104 68.866302 186.9183    56.41908
## 3 2000-01-06 70.822899 71.214104 69.649002 182.8103    55.17915
## 4 2000-01-07 67.301300 68.475197 66.518600 177.6750    53.62911
## 5 2000-01-10 69.649002 70.431396 68.475197 183.8371    55.48906
## 6 2000-01-11 70.822899 71.605202 68.475197 180.7559    54.55905
## # ... with 1 more variables: Volume <chr>
class(X2330_TW)
## [1] "tbl_df"     "tbl"        "data.frame"
str(X2330_TW)
## Classes 'tbl_df', 'tbl' and 'data.frame':    4399 obs. of  7 variables:
##  $ Date     : Date, format: "2000-01-04" "2000-01-05" ...
##  $ Open     : chr  "69.649002" "69.649002" "70.822899" "67.301300" ...
##  $ High     : chr  "69.649002" "71.214104" "71.214104" "68.475197" ...
##  $ Low      : chr  "68.475197" "68.866302" "69.649002" "66.518600" ...
##  $ Close    : num  183 187 183 178 184 ...
##  $ Adj Close: num  55.2 56.4 55.2 53.6 55.5 ...
##  $ Volume   : chr  "200662321971" "402466776297" "197545701266" "235270327441" ...
##  - attr(*, "problems")=Classes 'tbl_df', 'tbl' and 'data.frame': 58 obs. of  5 variables:
##   ..$ row     : int  4163 4163 4169 4169 4170 4170 4171 4171 4172 4172 ...
##   ..$ col     : chr  "Close" "Adj Close" "Close" "Adj Close" ...
##   ..$ expected: chr  "a double" "a double" "a double" "a double" ...
##   ..$ actual  : chr  "null" "null" "null" "null" ...
##   ..$ file    : chr  "'D:/OS DATA/Desktop/2330.TW.csv'" "'D:/OS DATA/Desktop/2330.TW.csv'" "'D:/OS DATA/Desktop/2330.TW.csv'" "'D:/OS DATA/Desktop/2330.TW.csv'" ...
##  - attr(*, "spec")=List of 2
##   ..$ cols   :List of 7
##   .. ..$ Date     :List of 1
##   .. .. ..$ format: chr ""
##   .. .. ..- attr(*, "class")= chr  "collector_date" "collector"
##   .. ..$ Open     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ High     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ Low      : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   .. ..$ Close    : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Adj Close: list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Volume   : list()
##   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr  "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
tw2330 <- X2330_TW[X2330_TW$`Adj Close` > 0 , ]
min(tw2330$`Adj Close`, na.rm=TRUE)
## [1] 21.75381
max(tw2330$`Adj Close`, na.rm=TRUE)
## [1] 218
hist(tw2330$`Adj Close`)

boxplot(tw2330$`Adj Close`)

head(tw2330[order(tw2330$`Adj Close`), ])
## # A tibble: 6 x 7
##         Date      Open      High       Low    Close `Adj Close`
##       <date>     <chr>     <chr>     <chr>    <dbl>       <dbl>
## 1 2002-10-08 26.995701 27.612900 26.918800 36.56203    21.75381
## 2 2002-10-11 28.538500 28.692301 27.381500 36.66443    21.81473
## 3 2002-10-07 28.923700 28.923700 27.766701 36.97255    21.99806
## 4 2002-10-09 27.458401 29.001301 27.227100 36.97255    21.99806
## 5 2002-10-03 30.312000 30.312000 29.078199 38.71886    23.03709
## 6 2002-10-04 28.384001 29.772301 28.384001 39.02685    23.22033
## # ... with 1 more variables: Volume <chr>
head(tw2330[order(tw2330$`Adj Close`, decreasing = TRUE), ])
## # A tibble: 6 x 7
##         Date       Open       High        Low Close `Adj Close`   Volume
##       <date>      <chr>      <chr>      <chr> <dbl>       <dbl>    <chr>
## 1 2017-06-22 217.000000 218.000000 216.500000 218.0       218.0 24139000
## 2 2017-06-23 218.000000 218.500000 217.000000 217.0       217.0 39621000
## 3 2017-06-20 215.000000 218.000000 214.500000 216.5       216.5 28630000
## 4 2017-06-21 216.000000 217.000000 214.500000 215.5       215.5 44789000
## 5 2017-06-08 212.000000 213.000000 211.500000 213.0       213.0 18437000
## 6 2017-06-19 211.000000 213.000000 210.500000 213.0       213.0 15632000
head(tw2330)
## # A tibble: 6 x 7
##         Date      Open      High       Low    Close `Adj Close`
##       <date>     <chr>     <chr>     <chr>    <dbl>       <dbl>
## 1 2000-01-04 69.649002 69.649002 68.475197 182.8103    55.17915
## 2 2000-01-05 69.649002 71.214104 68.866302 186.9183    56.41908
## 3 2000-01-06 70.822899 71.214104 69.649002 182.8103    55.17915
## 4 2000-01-07 67.301300 68.475197 66.518600 177.6750    53.62911
## 5 2000-01-10 69.649002 70.431396 68.475197 183.8371    55.48906
## 6 2000-01-11 70.822899 71.605202 68.475197 180.7559    54.55905
## # ... with 1 more variables: Volume <chr>
plot(tw2330$Date, tw2330$`Adj Close`)

## List

item <- list(thing='hat', size=8.25)
item
## $thing
## [1] "hat"
## 
## $size
## [1] 8.25
class(item)
## [1] "list"
class(item$thing)
## [1] "character"
class(item$size)
## [1] "numeric"
test <- list(name='toby', score= c(87, 57, 72))
test
## $name
## [1] "toby"
## 
## $score
## [1] 87 57 72
test$score
## [1] 87 57 72
test$score[2]
## [1] 57
li <- list(c(3,5,12), c(2,4,6,8,10))
li
## [[1]]
## [1]  3  5 12
## 
## [[2]]
## [1]  2  4  6  8 10
li[[1]]
## [1]  3  5 12
lapply(li, sum)
## [[1]]
## [1] 20
## 
## [[2]]
## [1] 30
lapply(li, function(e) sum(e) )
## [[1]]
## [1] 20
## 
## [[2]]
## [1] 30
lapply(li, function(e) mean(e) )
## [[1]]
## [1] 6.666667
## 
## [[2]]
## [1] 6
lapply(li, function(e) e[1] )
## [[1]]
## [1] 3
## 
## [[2]]
## [1] 2

Flow Control

x <- 2;

if(x > 3){ 
    print("x > 3");
}else{
    print("x <= 3");
}
## [1] "x <= 3"
x = 1;
if(x > 3){ 
    print("x > 3");
} else if(x ==3){
    print("x == 3");
}else{
    print("x < 3");
}
## [1] "x < 3"
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
}
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"
for(i in seq_along(x)){
  print(x[i])
}
## [1] "sunny"
## [1] "rainy"
## [1] "cloudy"
## [1] "rainy"
## [1] "cloudy"
for(i in x){
  print(i)
}
## [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
?paste

paste('hello', 'world')
## [1] "hello world"
paste0('hello', 'world')
## [1] "helloworld"
paste('hello', 'world', sep ='')
## [1] "helloworld"
url <- 'http://www.appledaily.com.tw/realtimenews/section/new/'

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

# def funName(arg1):
#     retunr arg1 + 3

f <- function(a){
  a + 3
}

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

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

f3(3)
## [1] 13
#f3(b = 5)
f3(b = 3, a = 2)
## [1] 17
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, 3)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
#head(3, iris)
head(n = 3, x = 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
f = function(a, b) {
   a * 2
}
f(3)
## [1] 6
#f = function(a, b) {
#     print(a+ b)
#}
#f(3)



article <- 'Xi Jinping to visit Hong Kong for 20th anniversary of handover By James Griffiths, CNN Updated 0739 GMT (1539 HKT) June 25, 2017 Chinese President Xi Jinping will visit Hong Kong for the first time as leader to mark the 20th anniversary of the city 39;s handover to China on July 1, 2017. 
Chinese President Xi Jinping will visit Hong Kong for the first time as leader to mark the 20th anniversary of the city s handover to China on July 1, 2017.
Story highlights
Chinese President Xi Jinping to visit Hong Kong for the first time as Chinese leader
His visit is expected to be met with massive protests
Hong Kong (CNN)Chinese President Xi Jinping will visit Hong Kong for the 20th anniversary of the city s handover to Chinese sovereignty, according to Chinese state news agency Xinhua.

Xi will visit Hong Kong between June 29 and July 1. It will be his first visit to the territory as Chinese leader, and is expected to be marked by massive protests.
During his time in the city, Xi will inspect People s Liberation Army troops at their garrison, visit a Chinese-Hong Kong construction project, and swear in Carrie Lam as the next Hong Kong Chief Executive.
He will also attend a major gala celebration to mark 20 years since China assumed control over the city.
Any visit by Xi Jinping to Hong Kong is expected to be marked by mass protests. 
Any visit by Xi Jinping to Hong Kong is expected to be marked by mass protests.
Heightened security
Xi s visit is expected to be accompanied by a massive security operation, with parts of the city put into lockdown to ensure protesters cannot get to the Chinese leader.
Hong Kong police have been drilling with their counterparts from Guangdong, across the border in China, for months.
According to the Ming Pao newspaper, front line officers have been told to block  sensitive images and words  from appearing in Xi s line of sight during events, such as signs referencing the Tiananmen Square massacre or expressing a desire for  genuine universal suffrage. 
During a visit by Zhang Dejiang, China s third most senior government official, to Hong Kong last year, police glued down sidewalks and erected huge barriers to prevent the public getting anywhere near Zhang.
A spokeswoman for Demosisto, the party founded by pro-democracy protesters Joshua Wong and Nathan Law, said they expected the police presence would be even heavier for Xi s visit.
She said the group was  alarmed  by reports about preventing certain protest materials.  Freedom of speech and political expression is enshrined in Basic Law,  she added, referring to Hong Kong s mini-constitution.
Thousands of Hong Kongers take to the streets every year on July 1 to call for democracy. 
Thousands of Hong Kongers take to the streets every year on July 1 to call for democracy.
Protests and marches
Thousands of Hong Kongers are expected to take to the streets during Xi s visit, particularly on July 1, the date of handover celebrations and a traditional day of protest in the city.
However, an annual pro-democracy rally was denied use of its usual staging ground in Victoria Park, in the heart of the city. The space has instead been promised to a pro-Beijing organization, the Hong Kong Celebrations Association, that will hold a handover commemoration event in the park, local media reported.
While the march will still go ahead, organizers said this was an attempt to crush dissent ahead of Xi s visit.
The global fight for Hong Kong democracy

The global fight for Hong Kong democracy 01:48
 The Chinese regime is trying to squeeze out the space that we have in Hong Kong and is a threat to our freedom and democracy,  said Lee Cheuk-yan, a longtime pro-democracy activist.
Earlier this year, Joshua Wong told CNN Xi s visit would be a  critical moment to organize civil disobedience and to voice our demand for democracy and human rights. 
In a statement, Law said Friday that Demosisto would use the anniversary of handover to  expose the facade of the celebrations for a peaceful China and the happy return of Hong Kong to the  motherland . '

wordcount <- function(article){
    seg <- strsplit(article , ' ')
    tb <- table(seg)
    head(tb[order(tb, decreasing = TRUE)], 10)
}

wordcount(article)
## seg
##   the    to  Hong  Kong    of   for   and visit     a    Xi 
##    38    38    19    19    18    15    14    14    13    13

tapply example

tw2330$day <- format(tw2330$Date, '%Y-%m-01') 

head(tw2330)
## # A tibble: 6 x 8
##         Date      Open      High       Low    Close `Adj Close`
##       <date>     <chr>     <chr>     <chr>    <dbl>       <dbl>
## 1 2000-01-04 69.649002 69.649002 68.475197 182.8103    55.17915
## 2 2000-01-05 69.649002 71.214104 68.866302 186.9183    56.41908
## 3 2000-01-06 70.822899 71.214104 69.649002 182.8103    55.17915
## 4 2000-01-07 67.301300 68.475197 66.518600 177.6750    53.62911
## 5 2000-01-10 69.649002 70.431396 68.475197 183.8371    55.48906
## 6 2000-01-11 70.822899 71.605202 68.475197 180.7559    54.55905
## # ... with 2 more variables: Volume <chr>, day <chr>
tapply(tw2330$Close, tw2330$day, mean)
## 2000-01-01 2000-02-01 2000-03-01 2000-04-01 2000-05-01 2000-06-01 
##  191.64257  187.95485  202.54662  191.01509  164.92201  153.52419 
## 2000-07-01 2000-08-01 2000-09-01 2000-10-01 2000-11-01 2000-12-01 
##  141.85124  135.00116  121.28192   99.53312  103.82264   87.73353 
## 2001-01-01 2001-02-01 2001-03-01 2001-04-01 2001-05-01 2001-06-01 
##   96.44383   97.70229   89.98682   87.50238   94.90630   88.47793 
## 2001-07-01 2001-08-01 2001-09-01 2001-10-01 2001-11-01 2001-12-01 
##   64.13992   67.82796   59.41308   56.77475   73.04092   86.00108 
## 2002-01-01 2002-02-01 2002-03-01 2002-04-01 2002-05-01 2002-06-01 
##   91.25087   86.05604   95.39091   95.21967   88.39410   75.51188 
## 2002-07-01 2002-08-01 2002-09-01 2002-10-01 2002-11-01 2002-12-01 
##   65.03726   52.19718   47.41254   43.01818   49.96217   49.20422 
## 2003-01-01 2003-02-01 2003-03-01 2003-04-01 2003-05-01 2003-06-01 
##   48.96744   43.77677   45.94210   46.77164   50.30445   59.05363 
## 2003-07-01 2003-08-01 2003-09-01 2003-10-01 2003-11-01 2003-12-01 
##   56.69768   58.30209   64.73249   64.75101   63.21447   59.70289 
## 2004-01-01 2004-02-01 2004-03-01 2004-04-01 2004-05-01 2004-06-01 
##   63.39672   60.17151   59.06202   58.22364   53.02648   46.50163 
## 2004-07-01 2004-08-01 2004-09-01 2004-10-01 2004-11-01 2004-12-01 
##   41.57942   42.89642   42.58048   41.25970   45.41203   46.85541 
## 2005-01-01 2005-02-01 2005-03-01 2005-04-01 2005-05-01 2005-06-01 
##   46.16966   51.31694   49.36266   48.89202   52.40077   53.78653 
## 2005-07-01 2005-08-01 2005-09-01 2005-10-01 2005-11-01 2005-12-01 
##   52.83187   50.35191   50.10759   50.70108   55.72580   61.02832 
## 2006-01-01 2006-02-01 2006-03-01 2006-04-01 2006-05-01 2006-06-01 
##   62.89372   61.60406   59.97169   66.44298   62.68162   57.17659 
## 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01 2006-12-01 
##   54.74682   56.11693   58.10851   60.59942   61.15734   64.89521 
## 2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01 
##   67.31016   66.90531   66.87782   67.57845   67.04559   67.24332 
## 2007-07-01 2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 
##   69.50484   61.11518   61.14910   62.34960   60.42352   59.71316 
## 2008-01-01 2008-02-01 2008-03-01 2008-04-01 2008-05-01 2008-06-01 
##   55.28436   60.74560   62.53239   64.22015   66.77539   64.81418 
## 2008-07-01 2008-08-01 2008-09-01 2008-10-01 2008-11-01 2008-12-01 
##   58.79724   58.84215   53.69817   45.51069   41.41792   42.23664 
## 2009-01-01 2009-02-01 2009-03-01 2009-04-01 2009-05-01 2009-06-01 
##   42.16609   44.25622   48.91453   51.56599   56.31841   57.82338 
## 2010-04-01 2010-07-01 2011-02-01 2011-03-01 2012-01-01 2012-02-01 
##   62.40000   60.20000   80.00000   65.60000   71.70000   79.10000 
## 2015-02-01 2016-06-01 2016-07-01 2016-08-01 2016-09-01 2016-10-01 
##  145.50000  163.35000  167.00000  175.12500  181.02500  188.60000 
## 2016-11-01 2016-12-01 2017-01-01 2017-02-01 2017-03-01 2017-04-01 
##  183.56818  182.31818  182.68750  187.02941  188.95652  190.41667 
## 2017-05-01 2017-06-01 
##  203.42500  211.58824