R Demo

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
plot(y1 ~ x1, data = anscombe)
abline(fit, col = "red")

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

R Basic

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

numer <- 17.8
char  <- 'Hello World'
bool  <- TRUE

class(numer)
## [1] "numeric"
class(char)
## [1] "character"
class(bool)
## [1] "logical"
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
RRP * Exchange 
## [1] 1142.323

Vector

depo <- 2000
depos <- c(2000,3000,1000,4000,5000)
names_vec <- c('John', 'William', 'Joe', 'May', 'Linda')

class(depos)
## [1] "numeric"
class(names_vec)
## [1] "character"
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 + 10
## [1] 11 12 13 17
x + c(10)
## [1] 11 12 13 17
x + c(10,10,10,10)
## [1] 11 12 13 17
1:20
##  [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
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  = 2)
## [1]  1 10
seq(1,10,length = 2)
## [1]  1 10
seq(1,10,len = 2)
## [1]  1 10
#?seq

x <- c(1,2,3,5,7)
sum(x)
## [1] 18
mean(x)
## [1] 3.6
median(x)
## [1] 3
max(x)
## [1] 7
min(x)
## [1] 1
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     2.0     3.0     3.6     5.0     7.0
portfolio1 <- c(-5,7,2,3,-2)
portfolio2 <- c(10,-2,-13,15,7)

mean(portfolio1)
## [1] 1
mean(portfolio2)
## [1] 3.4
sd(portfolio1) 
## [1] 4.636809
sd(portfolio2)
## [1] 11.05893
depos <- c(2000,3000,1000,4000,5000)
names_vec <- c('John', 'William', 'Joe', 'May', 'Linda')
names(depos) <- names_vec
depos
##    John William     Joe     May   Linda 
##    2000    3000    1000    4000    5000
depos > 3000
##    John William     Joe     May   Linda 
##   FALSE   FALSE   FALSE    TRUE    TRUE
depos >= 3000
##    John William     Joe     May   Linda 
##   FALSE    TRUE   FALSE    TRUE    TRUE
depos < 3000
##    John William     Joe     May   Linda 
##    TRUE   FALSE    TRUE   FALSE   FALSE
depos <= 3000
##    John William     Joe     May   Linda 
##    TRUE    TRUE    TRUE   FALSE   FALSE
depos == 3000
##    John William     Joe     May   Linda 
##   FALSE    TRUE   FALSE   FALSE   FALSE
depos != 3000
##    John William     Joe     May   Linda 
##    TRUE   FALSE    TRUE    TRUE    TRUE
depos[depos > 3000]
##   May Linda 
##  4000  5000
(depos >= 4000) | (depos <= 2000)
##    John William     Joe     May   Linda 
##    TRUE   FALSE    TRUE    TRUE    TRUE
(depos >= 2000) & (depos <= 4000)
##    John William     Joe     May   Linda 
##    TRUE    TRUE   FALSE    TRUE   FALSE
depos[(depos >= 2000) & (depos <= 4000)]
##    John William     May 
##    2000    3000    4000
depos[3]
##  Joe 
## 1000
depos[c(2,3,4)]
## William     Joe     May 
##    3000    1000    4000
2:4
## [1] 2 3 4
depos[2:4]
## William     Joe     May 
##    3000    1000    4000
## NONO
brian <- 73 / (1.80 )^2 
brian
## [1] 22.53086
topy <- 87 / (1.69 ) ^ 2

## Correct Method
weight <- c(73, 87, 43)
height <- c(180,169,173)
names_vec <- c('Brian', 'Toby', 'Sherry')

bmi <- weight / (height / 100) ^ 2
names(bmi) <- names_vec
bmi[(bmi < 18.5) | (bmi >=24)]
##     Toby   Sherry 
## 30.46112 14.36734
weight <- c(73, 87, 43)
height <- c(180,169,173)
names_vec <- c('Brian', 'Toby', 'Sherry')
names(weight) <- names_vec
weight
##  Brian   Toby Sherry 
##     73     87     43
height
## [1] 180 169 173
weight / (height / 100) ^ 2
##    Brian     Toby   Sherry 
## 22.53086 30.46112 14.36734
#weight / height

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)
mat
##      [,1] [,2]
## [1,]   85   73
## [2,]   72   64
## [3,]   59   66
rownames(mat) <- c('kevin', 'marry', 'jerry')
colnames(mat) <- c('first', 'second')
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
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat2[   ,  ]
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat2[ 1 ,  ]
##  first second 
##     85     73
mat2[   , 1]
## kevin marry jerry 
##    85    72    59
mat2[ 1:2 , 1]
## kevin marry 
##    85    72
mat2[ 1 , 2]
## [1] 73
mat2[,'first']
## kevin marry jerry 
##    85    72    59
mat2[,c('first', 'second')]
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat2[c('kevin', 'marry'),c('first', 'second')]
##       first second
## kevin    85     73
## marry    72     64
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat3 <- rbind(mat2 , c(78,63))

nrow(mat3)
## [1] 4
rownames(mat3)[4] <- 'sam'
rownames(mat3)[nrow(mat3)] <- 'sam'
mat3
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
## sam      78     63
mat2
##       first second
## kevin    85     73
## marry    72     64
## jerry    59     66
mat4 <- cbind(mat2, c(82,77,70) )
mat4
##       first second   
## kevin    85     73 82
## marry    72     64 77
## jerry    59     66 70
colnames(mat4)
## [1] "first"  "second" ""
colnames(mat4)[3] <- '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, nrow = 2, byrow = TRUE)
m2 <- matrix(5:8, nrow = 2, byrow = TRUE)
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
matrix(1:9,nrow = 9) %*% matrix(1:9,nrow = 1)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    2    3    4    5    6    7    8    9
##  [2,]    2    4    6    8   10   12   14   16   18
##  [3,]    3    6    9   12   15   18   21   24   27
##  [4,]    4    8   12   16   20   24   28   32   36
##  [5,]    5   10   15   20   25   30   35   40   45
##  [6,]    6   12   18   24   30   36   42   48   54
##  [7,]    7   14   21   28   35   42   49   56   63
##  [8,]    8   16   24   32   40   48   56   64   72
##  [9,]    9   18   27   36   45   54   63   72   81
kevin <- c(85,73)
kevin[1]
## [1] 85
kevin[2]
## [1] 73
kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)

mat <- matrix(c(kevin,marry,jerry), nrow = 3, byrow=TRUE)

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

Factor

weather <- c('sunny', 'rainy', 'cloudy', 'rainy', 'cloudy')
class(weather)
## [1] "character"
weather_factor <- factor(weather)
class(weather_factor)
## [1] "factor"
weather_factor
## [1] sunny  rainy  cloudy rainy  cloudy
## Levels: cloudy rainy sunny
levels(weather_factor)
## [1] "cloudy" "rainy"  "sunny"
weather
## [1] "sunny"  "rainy"  "cloudy" "rainy"  "cloudy"
weather[3] > weather[1]
## [1] FALSE
temperature <- c('Low', 'High', 'High', 'Medium', 'Low', 'Medium')
temperature_factor <- factor(temperature, order = TRUE, levels = c('Low', 'Medium', 'High') )

temperature_factor
## [1] Low    High   High   Medium Low    Medium
## Levels: Low < Medium < High
temperature_factor[3] > temperature_factor[1]
## [1] TRUE
temperature_factor[4] > temperature_factor[3]
## [1] FALSE
levels(temperature_factor)
## [1] "Low"    "Medium" "High"
weather <- c('s', 'r', 'c', 'r' ,'c' )
weather_factor <- factor(weather)
levels(weather_factor) <- c('cloudy', 'rainy' , 'sunny')
weather_factor
## [1] sunny  rainy  cloudy rainy  cloudy
## Levels: cloudy rainy sunny

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)
class(mat)
## [1] "matrix"
mat
##      [,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 = days , temp = temp, rain = 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(iris)
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
## starting httpd help server ... done
head(iris, n= 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
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
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[  1 ,   ])
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
head(iris[  1:3 ,   ])
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
head(iris[  c(1,3,5) ,   ])
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
head(iris[   ,   1])
## [1] 5.1 4.9 4.7 4.6 5.0 5.4
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
head(iris[   ,   'Sepal.Length'])
## [1] 5.1 4.9 4.7 4.6 5.0 5.4
head(iris[   ,   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
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 ...
head(iris$Species)
## [1] setosa setosa setosa setosa setosa setosa
## Levels: setosa versicolor virginica
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[iris$Species == 'setosa' & iris$Sepal.Length >= 5, c('Sepal.Length', 'Species') ] )
##    Sepal.Length Species
## 1           5.1  setosa
## 5           5.0  setosa
## 6           5.4  setosa
## 8           5.0  setosa
## 11          5.4  setosa
## 15          5.8  setosa
head(iris[iris$Species == 'setosa',        ])
##   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[which(iris$Species == 'setosa'), ])
##   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
heights <- c(180,169,173,177,182,156)
sort(heights)
## [1] 156 169 173 177 180 182
sort(heights, decreasing = TRUE)
## [1] 182 180 177 173 169 156
head(sort(iris$Sepal.Length, decreasing = TRUE))
## [1] 7.9 7.7 7.7 7.7 7.7 7.6
order(heights)
## [1] 6 2 3 4 1 5
order(heights, decreasing = TRUE)
## [1] 5 1 4 3 2 6
heights[order(heights, decreasing = TRUE)]
## [1] 182 180 177 173 169 156
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
tb <- table(iris$Species)
pie(tb)

barplot(tb, col="blue")

hist(iris$Sepal.Length)

boxplot(iris$Sepal.Length)

boxplot(Sepal.Length ~ Species, data = iris)

boxplot(Petal.Length ~ Species, data = iris)

plot(Petal.Length ~ Petal.Width, data = iris , col=iris$Species)

download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/2330_TW.csv', '2330_TW.csv')
getwd()
## [1] "D:/OS DATA/Desktop"
library(readr)
tw2330 <- read_csv("D:/OS DATA/Desktop/2330_TW.csv", 
    na = "null")
## Parsed with column specification:
## cols(
##   Date = col_date(format = ""),
##   Open = col_double(),
##   High = col_double(),
##   Low = col_double(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_integer()
## )
View(tw2330)

str(tw2330)
## Classes 'tbl_df', 'tbl' and 'data.frame':    1229 obs. of  7 variables:
##  $ Date     : Date, format: "2012-11-06" "2012-11-07" ...
##  $ Open     : num  90.6 90.5 90.8 90.3 91 ...
##  $ High     : num  90.6 91 90.9 90.9 91.8 ...
##  $ Low      : num  89.7 90 89.9 89.5 90.6 ...
##  $ Close    : num  90.4 91 90.5 90.8 91.3 ...
##  $ Adj Close: num  77.6 78.1 77.7 78 78.4 ...
##  $ Volume   : int  26333000 22025000 39566000 36639000 29882000 37000000 30190000 25043000 32472000 23037000 ...
##  - 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_double" "collector"
##   .. ..$ High     : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Low      : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Close    : list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Adj Close: list()
##   .. .. ..- attr(*, "class")= chr  "collector_double" "collector"
##   .. ..$ Volume   : list()
##   .. .. ..- attr(*, "class")= chr  "collector_integer" "collector"
##   ..$ default: list()
##   .. ..- attr(*, "class")= chr  "collector_guess" "collector"
##   ..- attr(*, "class")= chr "col_spec"
max(tw2330[tw2330$Date >= '2017-01-01' & tw2330$Date < '2017-07-01', 'Close'], na.rm=TRUE)
## [1] 218
min(tw2330[tw2330$Date >= '2017-01-01' & tw2330$Date < '2017-07-01', 'Close'], na.rm=TRUE)
## [1] 179.5
summary(tw2330[tw2330$Date >= '2017-01-01' & tw2330$Date < '2017-07-01', 'Close'], na.rm=TRUE)
##      Close      
##  Min.   :179.5  
##  1st Qu.:186.0  
##  Median :191.2  
##  Mean   :194.8  
##  3rd Qu.:205.1  
##  Max.   :218.0  
##  NA's   :2
stock <- tw2330[tw2330$Date >= '2017-01-01' & tw2330$Date < '2017-07-01', ]

head(stock[order(stock$Close),])
## # A tibble: 6 x 7
##         Date  Open  High   Low Close `Adj Close`   Volume
##       <date> <dbl> <dbl> <dbl> <dbl>       <dbl>    <int>
## 1 2017-01-16 180.0 180.5 179.0 179.5    173.7097 30756000
## 2 2017-01-19 179.5 181.0 179.5 180.5    174.6774 24627000
## 3 2017-01-17 180.5 181.0 179.5 181.0    175.1613 13159000
## 4 2017-01-18 180.5 181.0 179.5 181.0    175.1613 23693000
## 5 2017-01-20 181.0 181.5 180.5 181.0    175.1613 23429000
## 6 2017-01-13 180.5 182.5 180.5 181.5    175.6452 52352000
head(stock[order(stock$Close, decreasing = TRUE),])
## # A tibble: 6 x 7
##         Date  Open  High   Low Close `Adj Close`   Volume
##       <date> <dbl> <dbl> <dbl> <dbl>       <dbl>    <int>
## 1 2017-06-22   217 218.0 216.5 218.0    210.9677 24139000
## 2 2017-06-23   218 218.5 217.0 217.0    210.0000 39621000
## 3 2017-06-20   215 218.0 214.5 216.5    209.5161 28630000
## 4 2017-06-21   216 217.0 214.5 215.5    208.5484 44789000
## 5 2017-06-26   212 215.0 212.0 215.0    215.0000 58686000
## 6 2017-06-08   212 213.0 211.5 213.0    206.1290 18437000
a <- c(1,2,3,4,5)
max(a)
## [1] 5
a <- c(1,2,3,4,5, NA)
max(a, na.rm=TRUE)
## [1] 5
?max




download.file('https://raw.githubusercontent.com/ywchiu/fubonr/master/data/max_2330.csv', 'max_2330.csv')

library(readr)
max_2330 <- read_csv("D:/OS DATA/Desktop/max_2330.csv", 
    na = "null")
## Parsed with column specification:
## cols(
##   Date = col_date(format = ""),
##   Open = col_double(),
##   High = col_double(),
##   Low = col_double(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_double()
## )
View(max_2330)

min(max_2330$Close, na.rm=TRUE)
## [1] 27.4584
max(max_2330$Close, na.rm=TRUE)
## [1] 243
summary(max_2330$Close, na.rm=TRUE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   27.46   50.52   62.90   80.12   98.45  243.00     176
hist(max_2330$Close)

boxplot(max_2330$Close)

head(max_2330[order(max_2330$Close),],3)
## # A tibble: 3 x 7
##         Date    Open    High     Low   Close `Adj Close`       Volume
##       <date>   <dbl>   <dbl>   <dbl>   <dbl>       <dbl>        <dbl>
## 1 2002-10-08 26.9957 27.6129 26.9188 27.4584    16.15044 143018917018
## 2 2002-10-11 28.5385 28.6923 27.3815 27.5353    16.19567 142303912794
## 3 2002-10-07 28.9237 28.9237 27.7667 27.7667    16.33177  78537822777
head(max_2330[order(max_2330$Close,decreasing = TRUE),],3)
## # A tibble: 3 x 7
##         Date  Open  High   Low Close `Adj Close`   Volume
##       <date> <dbl> <dbl> <dbl> <dbl>       <dbl>    <dbl>
## 1 2017-10-30 242.0   245 241.5 243.0       243.0 27784990
## 2 2017-10-31 243.5   245 241.5 243.0       243.0 22582013
## 3 2017-11-01 243.5   245 241.5 242.5       242.5 20389054
plot(max_2330$Date, max_2330$Close, type= 'l')