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
a <- 3
a
[1] 3
a = 3
b <- 5
a + b
[1] 8
a <- 8
b <- 5
a + b
[1] 13
numer <- 17.8
char <- "hello world"
logic <- TRUE
class(numer)
[1] "numeric"
class(char)
[1] "character"
card_length <- 3
card_width <- "5 inches"
card_width <- 5
card_length * card_width
[1] 15
RRP <- 35.99
Exchange <- 31.74
NTD <- RRP * Exchange
NTD
[1] 1142.323
height_vec <- c(180,169,173)
names_vec <- c("Brian", "Toby", "Sherry")
height_vec[1]
[1] 180
height_vec[3]
[1] 173
height_vec[2:3]
[1] 169 173
x <- c(1,2,3,7)
y <- c(2,3,5,1)
x+y
[1] 3 5 8 8
x*y
[1] 2 6 15 7
x - y
[1] -1 -1 -2 6
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
x <- 1:20
x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[15] 15 16 17 18 19 20
y <- seq(1,20)
y <- seq(from = 1, to = 20)
y
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14
[15] 15 16 17 18 19 20
?seq
help(seq)
seq(1,20,2)
[1] 1 3 5 7 9 11 13 15 17 19
seq(1,3.5, by =0.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5
seq(1,10,length=2)
[1] 1 10
x <- c(1,2,3,5,7)
sum(x)
[1] 18
min(x)
[1] 1
max(x)
[1] 7
summary(x)
Min. 1st Qu. Median Mean 3rd Qu.
1.0 2.0 3.0 3.6 5.0
Max.
7.0
?sum
height_vec <- c(180,169,173)
height_vec
[1] 180 169 173
names(height_vec) <- c("Brian", "Toby", "Sherry")
height_vec
Brian Toby Sherry
180 169 173
name_vec <- c("Brian", "Toby", "Sherry")
names(height_vec) <- name_vec
height_vec
Brian Toby Sherry
180 169 173
height_vec[1]
Brian
180
height_vec > 175
Brian Toby Sherry
TRUE FALSE FALSE
height_vec[height_vec > 175]
Brian
180
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
FALSE FALSE FALSE
height_vec != 175
Brian Toby Sherry
TRUE TRUE TRUE
(height_vec >= 170) & (height_vec < 180)
Brian Toby Sherry
FALSE FALSE TRUE
(height_vec >= 180) | (height_vec <= 170)
Brian Toby Sherry
TRUE TRUE FALSE
height_vec <- c(180,169,173)
weight_vec <- c(73 ,87 ,43 )
bmi_vec <- weight_vec / ((height_vec / 100) ^ 2)
names_vec <- c('Brian', 'Toby', 'Sherry')
names(bmi_vec) <- names_vec
bmi_vec < 18.5 | bmi_vec >=24
Brian Toby Sherry
FALSE TRUE TRUE
bmi_vec[bmi_vec < 18.5 | bmi_vec >=24]
Toby Sherry
30.46112 14.36734
names(bmi_vec[bmi_vec < 18.5 | bmi_vec >=24])
[1] "Toby" "Sherry"
test1 <- c(60,53,80)
test2 <- c(80,63,90)
test3 <- c(70,58,85)
c(test1,test2,test3)
[1] 60 53 80 80 63 90 70 58 85
matrix(c(test1,test2,test3), nrow=3, byrow = TRUE)
[,1] [,2] [,3]
[1,] 60 53 80
[2,] 80 63 90
[3,] 70 58 85
matrix(1:9, byrow=TRUE, nrow=3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
matrix(1:9, nrow=3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 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
first second
kevin 85 73
marry 72 64
jerry 59 66
mat2[,1]
kevin marry jerry
85 72 59
mat2[1,]
first second
85 73
mat2[2:3,]
first second
marry 72 64
jerry 59 66
mat2[2,1]
[1] 72
mat3 <- rbind(mat2, c(78,63))
rownames(mat3)[4] <- 'sam'
rownames(mat3)[nrow(mat3)] <- 'sam'
mat4 <- cbind(mat2, c(82,77,70))
colnames(mat4)[ncol(mat4)] <- 'third'
mat4
first second third
kevin 85 73 82
marry 72 64 77
jerry 59 66 70
mat2
first second
kevin 85 73
marry 72 64
jerry 59 66
rowSums(mat2)
kevin marry jerry
158 136 125
rowMeans(mat2)
kevin marry jerry
79.0 68.0 62.5
colSums(mat2)
first second
216 203
colMeans(mat2)
first second
72.00000 67.66667
m1 <- matrix(1:4, byrow=TRUE, nrow=2)
m2 <- matrix(5:8, byrow=TRUE, nrow=2)
m1 + m2
[,1] [,2]
[1,] 6 8
[2,] 10 12
m1 - m2
[,1] [,2]
[1,] -4 -4
[2,] -4 -4
m1 * m2
[,1] [,2]
[1,] 5 12
[2,] 21 32
m1 / m2
[,1] [,2]
[1,] 0.2000000 0.3333333
[2,] 0.4285714 0.5000000
m1 %*% m2
[,1] [,2]
[1,] 19 22
[2,] 43 50
kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)
mat <- matrix(c(kevin, marry, jerry), nrow=3,
byrow= TRUE)
colnames(mat) <- c('first', 'second')
rownames(mat) <- c('kevin', 'marry', 'jerry')
# method 1
test_avg <- mat[,1] * 0.4 + mat[,2] * 0.6
cbind(mat, test_avg)
first second test_avg
kevin 85 73 77.8
marry 72 64 67.2
jerry 59 66 63.2
# method 2
mat %*% c(0.4,0.6)
[,1]
kevin 77.8
marry 67.2
jerry 63.2
# 9 * 9
matrix(1:9, nrow = 9) %*% matrix(1:9, nrow = 1)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 2 4 6 8 10 12 14 16
[3,] 3 6 9 12 15 18 21 24
[4,] 4 8 12 16 20 24 28 32
[5,] 5 10 15 20 25 30 35 40
[6,] 6 12 18 24 30 36 42 48
[7,] 7 14 21 28 35 42 49 56
[8,] 8 16 24 32 40 48 56 64
[9,] 9 18 27 36 45 54 63 72
[,9]
[1,] 9
[2,] 18
[3,] 27
[4,] 36
[5,] 45
[6,] 54
[7,] 63
[8,] 72
[9,] 81
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
class(weather_category)
[1] "factor"
levels(weather_category)
[1] "cloudy" "rainy" "sunny"
temperature <- c("Low", "High", "High", "Medium", "Low","Medium")
temperature
[1] "Low" "High" "High" "Medium"
[5] "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"
days <- c('mon','tue','wed','thu','fri')
temp <- c( 22.2, 21, 23, 24.3, 25)
rain <- c( TRUE, TRUE,FALSE,FALSE, TRUE)
df <- data.frame(days, temp, rain)
df
class(df)
[1] "data.frame"
str(df)
'data.frame': 5 obs. of 3 variables:
$ days: Factor w/ 5 levels "fri","mon","thu",..: 2 4 5 3 1
$ temp: num 22.2 21 23 24.3 25
$ rain: logi TRUE TRUE FALSE FALSE TRUE
summary(df)
days temp rain
fri:1 Min. :21.0 Mode :logical
mon:1 1st Qu.:22.2 FALSE:2
thu:1 Median :23.0 TRUE :3
tue:1 Mean :23.1 NA's :0
wed:1 3rd Qu.:24.3
Max. :25.0
data()
data(iris)
class(iris)
[1] "data.frame"
View(iris)
head(iris)
#?head
head(iris, 10)
tail(iris)
tail(iris, 10)
iris[1:3 , ]
iris[1:3 , 1]
[1] 5.1 4.9 4.7
iris[1:3 , "Sepal.Length"]
[1] 5.1 4.9 4.7
iris[ , 1:2]
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$Sepal.Length)
[1] 5.1 4.9 4.7 4.6 5.0 5.4
five.sepal.iris <- iris[1:5, c('Sepal.Length', 'Sepal.Width')]
# filter by true and false
iris[iris$Species == 'setosa', 1:3]
# filter by position
which(iris$Species == 'setosa')
[1] 1 2 3 4 5 6 7 8 9 10 11 12
[13] 13 14 15 16 17 18 19 20 21 22 23 24
[25] 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48
[49] 49 50
iris[which(iris$Species == 'setosa'), 1:3]
iris[iris$Species == 'setosa' & iris$Sepal.Length >= 5, 1:4]
# from small to large
sort(iris$Sepal.Length)
[1] 4.3 4.4 4.4 4.4 4.5 4.6 4.6 4.6 4.6
[10] 4.7 4.7 4.8 4.8 4.8 4.8 4.8 4.9 4.9
[19] 4.9 4.9 4.9 4.9 5.0 5.0 5.0 5.0 5.0
[28] 5.0 5.0 5.0 5.0 5.0 5.1 5.1 5.1 5.1
[37] 5.1 5.1 5.1 5.1 5.1 5.2 5.2 5.2 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118] 5.1 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0
[127] 5.0 5.0 4.9 4.9 4.9 4.9 4.9 4.9 4.8
[136] 4.8 4.8 4.8 4.8 4.7 4.7 4.6 4.6 4.6
[145] 4.6 4.5 4.4 4.4 4.4 4.3
a <- c(2,4,5,1,3)
sort(a)
[1] 1 2 3 4 5
order(a)
[1] 4 1 5 2 3
iris[order(iris$Sepal.Length, decreasing = TRUE), ]
#install.packages("quantmod")
library(quantmod)
getSymbols('2330.TW')
[1] "2330.TW"
View(`2330.TW`)
class(`2330.TW`)
[1] "xts" "zoo"
tw2330 <- as.data.frame(`2330.TW`)
str(tw2330)
'data.frame': 2509 obs. of 6 variables:
$ 2330.TW.Open : num 67.5 67.5 67.4 67.2 65.8 ...
$ 2330.TW.High : num 67.7 68.4 67.8 67.4 66.2 ...
$ 2330.TW.Low : num 67 67.2 67.3 66.2 65.4 ...
$ 2330.TW.Close : num 67.4 67.7 67.4 66.6 65.5 ...
$ 2330.TW.Volume : num 26160600 42250300 22331600 30750100 24228400 ...
$ 2330.TW.Adjusted: num 47.3 47.5 47.3 46.8 46 ...
max(tw2330$`2330.TW.Close`)
[1] 193
min(tw2330$`2330.TW.Close`)
[1] 36.8
summary(tw2330$`2330.TW.Close`)
Min. 1st Qu. Median Mean 3rd Qu.
36.80 62.30 76.60 92.15 122.00
Max.
193.00
tw2330[order(tw2330$`2330.TW.Close`),]
tw2330[order(tw2330$`2330.TW.Close`, decreasing = TRUE),]
hist(tw2330$`2330.TW.Close`)
boxplot(tw2330$`2330.TW.Close`)
plot(tw2330$`2330.TW.Close`)
tw2330$tf <- ifelse((tw2330$`2330.TW.Close` - tw2330$`2330.TW.Open`) > 0, 'bull', 'bear')
head(tw2330)
table(tw2330$tf)
bear bull
1435 1074
tw2330_close_2015 <- tw2330[rownames(tw2330) >= '2015-01-01' & rownames(tw2330) < '2016-01-01', 4]
max(tw2330_close_2015)
[1] 154.5
min(tw2330_close_2015)
[1] 115
summary(tw2330_close_2015)
Min. 1st Qu. Median Mean 3rd Qu.
115.0 136.5 141.0 140.1 146.0
Max.
154.5
item <- list(thing="hat", size=8.25)
item
$thing
[1] "hat"
$size
[1] 8.25
item$size
[1] 8.25
test <- list(name="Toby", score = c(87,57,72))
test$score
[1] 87 57 72
test$score[1]
[1] 87
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
lapply(li, sum)
[[1]]
[1] 20
[[2]]
[1] 29