Markdown Test

R Demo

data(anscombe)
View(anscombe)
summary(anscombe)
##        x1             x2             x3             x4    
##  Min.   : 4.0   Min.   : 4.0   Min.   : 4.0   Min.   : 8  
##  1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 6.5   1st Qu.: 8  
##  Median : 9.0   Median : 9.0   Median : 9.0   Median : 8  
##  Mean   : 9.0   Mean   : 9.0   Mean   : 9.0   Mean   : 9  
##  3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.:11.5   3rd Qu.: 8  
##  Max.   :14.0   Max.   :14.0   Max.   :14.0   Max.   :19  
##        y1               y2              y3              y4        
##  Min.   : 4.260   Min.   :3.100   Min.   : 5.39   Min.   : 5.250  
##  1st Qu.: 6.315   1st Qu.:6.695   1st Qu.: 6.25   1st Qu.: 6.170  
##  Median : 7.580   Median :8.140   Median : 7.11   Median : 7.040  
##  Mean   : 7.501   Mean   :7.501   Mean   : 7.50   Mean   : 7.501  
##  3rd Qu.: 8.570   3rd Qu.:8.950   3rd Qu.: 7.98   3rd Qu.: 8.190  
##  Max.   :10.840   Max.   :9.260   Max.   :12.74   Max.   :12.500
plot(y1 ~ x1 , data = anscombe)

plot(y1 ~ x2 , data = anscombe)

plot(y2 ~ x1 , data = anscombe)

plot(y3 ~ x1 , data = anscombe)

plot(y4 ~ x1 , data = 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 Arithmetic

3 + 8
## [1] 11
3 - 8
## [1] -5
5 * 5
## [1] 25
11 / 2
## [1] 5.5
2 ^ 10
## [1] 1024
11 %% 2
## [1] 1
a <- 3
a
## [1] 3
b = 5
a + b
## [1] 8
numer <- 17.8
char  <- 'Hello World'
char2 <- "Hello World"
logic <- TRUE

class(numer)
## [1] "numeric"
class(char)
## [1] "character"
card_length <- 3
card_width  <- '5 inches'
# This is a comment
# card_length * card_width
card_width <- 5
card_length * card_width
## [1] 15
RRP <- 35.99
RRP
## [1] 35.99
# rrp

Exchange <- 31.74
RRP * Exchange 
## [1] 1142.323

R Vector

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


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
## 1 => 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
# get item by position
x
## [1] 1 2 3 4
x[1]
## [1] 1
x[4]
## [1] 4
#use : or seq to generate vectors with multiple continuous elements c(1,2,3,4,5..)
1:20
##  [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
# use help or ? to seek for help doc
help(seq)
## starting httpd help server ... done
?seq

seq()
## [1] 1
seq(0, 1, length.out = 11)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
seq(1, 9, by = 2) 
## [1] 1 3 5 7 9
seq(from = 1, to = 20, by = 2)
##  [1]  1  3  5  7  9 11 13 15 17 19
seq(1,20,2)
##  [1]  1  3  5  7  9 11 13 15 17 19
seq(1,3.5, by =0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5
seq(1,10, length.out = 2)
## [1]  1 10
seq(1,10, length=2)
## [1]  1 10
seq(1,10, len=2)
## [1]  1 10
## get descriptive statstics
x <- c(1,2,3,5,7)
sum(x)
## [1] 18
mean(x)
## [1] 3.6
max(x)
## [1] 7
min(x)
## [1] 1
median(x)
## [1] 3
## use summary to get descriptive statistics summary
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     2.0     3.0     3.6     5.0     7.0
## Missing Value: NA
x <- c(1,2,3,5,7 , NA)
sum(x)
## [1] NA
?sum
sum(3,5,7)
## [1] 15
sum(x, na.rm=TRUE)
## [1] 18
sum(1:100)
## [1] 5050
## add name to vector
height_vec <- c(180, 169, 173)
name_vec   <- c('Brian', 'Toby', 'Sherry')

names(height_vec) <- c('Brian', 'Toby', 'Sherry')
names(height_vec)
## [1] "Brian"  "Toby"   "Sherry"
height_vec
##  Brian   Toby Sherry 
##    180    169    173
names(height_vec) <- name_vec
height_vec
##  Brian   Toby Sherry 
##    180    169    173
# by pos
height_vec[1]
## Brian 
##   180
height_vec[c(1,2)]
## Brian  Toby 
##   180   169
height_vec[c(1,3)]
##  Brian Sherry 
##    180    173
height_vec[1:3]
##  Brian   Toby Sherry 
##    180    169    173
height_vec[2:3]
##   Toby Sherry 
##    169    173
# by name
height_vec['Brian']
## Brian 
##   180
height_vec[c('Toby', 'Sherry')]
##   Toby Sherry 
##    169    173
# by logic
height_vec[c(TRUE, FALSE, TRUE)]
##  Brian Sherry 
##    180    173
# with filter condition
height_vec > 175
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
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   TRUE   TRUE
height_vec != 175
##  Brian   Toby Sherry 
##   TRUE   TRUE   TRUE
height_vec == 180
##  Brian   Toby Sherry 
##   TRUE  FALSE  FALSE
which(height_vec == 180)
## Brian 
##     1
height_vec[height_vec >= 175]
## Brian 
##   180
height_vec[which(height_vec >= 175)]
## Brian 
##   180
height_vec >= 170 & height_vec < 180
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
height_vec < 170  | height_vec >= 180
##  Brian   Toby Sherry 
##   TRUE   TRUE  FALSE
logic <- c(TRUE, FALSE, TRUE)
logic
## [1]  TRUE FALSE  TRUE
# ! turn true to false, turn false to true
! logic
## [1] FALSE  TRUE FALSE
! (height_vec < 170  | height_vec >= 180)
##  Brian   Toby Sherry 
##  FALSE  FALSE   TRUE
height_vec[! (height_vec < 170  | height_vec >= 180)]
## Sherry 
##    173