R Basic
# 數字相加
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
c<-a + b
c
## [1] 8
# 數值型態
numer<-17.8
# 字串型態
char <-"hello world"
#布林邏輯
logic <-TRUE
# 使用class 檢查資料型態
class(logic)
## [1] "logical"
card_length<-3
card_width<-"5 inches"
#card_length*card_width
#重新將card_width指到5
card_width<-5
card_length*card_width
## [1] 15
RRP <-35.99
Exchange <-31.74
NTD <-RRP *Exchange
NTD
## [1] 1142.323
Vector
height <- 175
height1 <- 162
height2 <- 180
height_vec <- c(180, 169, 173)
name_vec <- c("Brian", 'Toby', 'Sherry')
height_vec
## [1] 180 169 173
name_vec
## [1] "Brian" "Toby" "Sherry"
height_vec[1]
## [1] 180
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 <- 1:20
y <- seq(1,20)
help(seq)
## starting httpd help server ... done
?seq
seq(1,9)
## [1] 1 2 3 4 5 6 7 8 9
seq(from=1, to = 9 )
## [1] 1 2 3 4 5 6 7 8 9
seq(9,1)
## [1] 9 8 7 6 5 4 3 2 1
seq(to=9, from = 1 )
## [1] 1 2 3 4 5 6 7 8 9
seq(1,9,2)
## [1] 1 3 5 7 9
seq(from=1, to=9, by = 2)
## [1] 1 3 5 7 9
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(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.out = 3)
## [1] 1.0 5.5 10.0
seq(1,10, length.out = 4)
## [1] 1 4 7 10
x <- c(1,2,3,5,7)
sum(x)
## [1] 18
sum(1:100)
## [1] 5050
?sum
y <- c(1,2,3,NA,7)
sum(y)
## [1] NA
sum(y, na.rm=TRUE)
## [1] 13
height_vec <- c(180, 169, 173)
height_vec
## [1] 180 169 173
name_vec <- c('Brian', 'Toby', 'Sherry')
name_vec
## [1] "Brian" "Toby" "Sherry"
names(height_vec) <- name_vec
height_vec
## Brian Toby Sherry
## 180 169 173
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
## FALSE FALSE FALSE
height_vec != 175
## Brian Toby Sherry
## TRUE TRUE TRUE
height_vec
## Brian Toby Sherry
## 180 169 173
height_vec > 175
## Brian Toby Sherry
## TRUE FALSE FALSE
height_vec[1]
## Brian
## 180
height_vec[c(TRUE, FALSE, FALSE)]
## Brian
## 180
height_vec[height_vec > 175]
## Brian
## 180
height_vec > 175
## Brian Toby Sherry
## TRUE FALSE FALSE
height_vec < 170
## Brian Toby Sherry
## FALSE TRUE FALSE
# | => OR
height_vec > 175 | height_vec < 170
## Brian Toby Sherry
## TRUE TRUE FALSE
# & => AND
height_vec > 170 & height_vec < 175
## Brian Toby Sherry
## FALSE FALSE TRUE
# ! => NOT
!(height_vec > 175)
## Brian Toby Sherry
## FALSE TRUE TRUE
name_vec <- c('Brian', 'Toby', 'Sherry')
height_vec <- c(180, 169, 173)
names(height_vec) <- name_vec
weight_vec <- c(73 , 87, 43)
bmi_vec <- weight_vec / (height_vec / 100) ^ 2
bmi_vec
## Brian Toby Sherry
## 22.53086 30.46112 14.36734
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"
Matrix
1:9
## [1] 1 2 3 4 5 6 7 8 9
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
?matrix
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')
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
kevin[2]
## [1] 73
mat2[ 1 , ]
## first second
## 85 73
mat2[ 'kevin' , ]
## first second
## 85 73
mat2[ ,1 ]
## kevin marry jerry
## 85 72 59
mat2[ ,'first']
## kevin marry jerry
## 85 72 59
mat2[ c(2,3), ]
## first second
## marry 72 64
## jerry 59 66
mat2[ 2:3 , ]
## first second
## marry 72 64
## jerry 59 66
mat2[ 2:3 , 1 ]
## marry jerry
## 72 59
mat2[ 2:3 ,'first']
## marry jerry
## 72 59
mat2[ 2 , 1 ]
## [1] 72
mat2[c('kevin', 'jerry'), c('first', 'second')]
## first second
## kevin 85 73
## jerry 59 66
mat3 <- rbind(mat2, c(78, 63))
mat3
## first second
## kevin 85 73
## marry 72 64
## jerry 59 66
## 78 63
rownames(mat3)
## [1] "kevin" "marry" "jerry" ""
rownames(mat3)[4]
## [1] ""
rownames(mat3)[4] <- 'sam'
nrow(mat3)
## [1] 4
mat2
## first second
## kevin 85 73
## marry 72 64
## jerry 59 66
mat4<- cbind(mat2, c(82,77,70))
ncol(mat4)
## [1] 3
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
colSums(mat2)
## first second
## 216 203
m1 <-matrix(1:4, byrow=TRUE, nrow=2)
m2 <-matrix(5:8, byrow=TRUE, nrow=2)
m1
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
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 %*% m2
## [,1] [,2]
## [1,] 19 22
## [2,] 43 50
# (9 X 1) %*% (1 X 9) = (9 X 9)
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)
marry <-c(72,64)
jerry <-c(59,66)
mat <- matrix(c(kevin, marry, jerry), nrow = 3, byrow =TRUE)
# method 1
mat[,1] * 0.4 + mat[,2] * 0.6
## [1] 77.8 67.2 63.2
# method 2
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"
w <- 'sunny'
w <- c('sunny')
class(w)
## [1] "character"
a <- c(1,2,3,4)
a + 3
## [1] 4 5 6 7
a + c(3,3,3,3)
## [1] 4 5 6 7
a <- c(1,2,3,4)
a + c(1,2)
## [1] 2 4 4 6
a + c(1,2,1,2)
## [1] 2 4 4 6
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
## [1] "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] FALSE
temperature_category[3] > temperature_category[1]
## [1] TRUE
temperature_category[4] > temperature_category[3]
## [1] FALSE
levels(temperature_category)
## [1] "Low" "Medium" "High"
DataFrame
a <- c(72,88)
b <- c('marry', 'John')
matrix(c(b,a), nrow = 2, byrow=TRUE)
## [,1] [,2]
## [1,] "marry" "John"
## [2,] "72" "88"
c(a, b)
## [1] "72" "88" "marry" "John"
# 建立Vector
days <-c('mon','tue','wed','thu','fri')
temp <-c(22.2,21,23,24.3,25)
rain <-c(TRUE, TRUE, FALSE, FALSE, TRUE)
# 使用Vector 建立Data Frame
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(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
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
head(iris[0:3])
## Sepal.Length Sepal.Width Petal.Length
## 1 5.1 3.5 1.4
## 2 4.9 3.0 1.4
## 3 4.7 3.2 1.3
## 4 4.6 3.1 1.5
## 5 5.0 3.6 1.4
## 6 5.4 3.9 1.7
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
#取前三列第一行的資料
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
#也可以用欄位名稱取值
head(iris$Sepal.Length)
## [1] 5.1 4.9 4.7 4.6 5.0 5.4
iris[ 1:5 , 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
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[iris$Species == 'setosa', 1:5])
## 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
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(1,5,2,4,8,6)
sort(a)
## [1] 1 2 4 5 6 8
order(a)
## [1] 1 3 4 2 6 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
## Analyze 2330.TW
library(readr)
X2330_TW <- read_csv("C:/Users/Administrator/Downloads/2330.TW.csv",na = c("", "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(X2330_TW)
?read_csv
class(X2330_TW)
## [1] "tbl_df" "tbl" "data.frame"
str(X2330_TW)
## Classes 'tbl_df', 'tbl' and 'data.frame': 4459 obs. of 7 variables:
## $ Date : Date, format: "2000-01-04" "2000-01-05" ...
## $ Open : num 69.6 69.6 70.8 67.3 69.6 ...
## $ High : num 69.6 71.2 71.2 68.5 70.4 ...
## $ Low : num 68.5 68.9 69.6 66.5 68.5 ...
## $ Close : num 69.6 71.2 69.6 67.7 70 ...
## $ Adj Close: num 53.4 54.6 53.4 51.9 53.7 ...
## $ Volume : num 2.01e+11 4.02e+11 1.98e+11 2.35e+11 2.76e+11 ...
## - 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_double" "collector"
## ..$ default: list()
## .. ..- attr(*, "class")= chr "collector_guess" "collector"
## ..- attr(*, "class")= chr "col_spec"
max(X2330_TW$Close, na.rm = TRUE)
## [1] 219
min(X2330_TW$Close, na.rm = TRUE)
## [1] 27.4584
summary(X2330_TW$Close, na.rm = TRUE)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 27.46 44.97 54.02 68.29 65.02 219.00 1883
boxplot(X2330_TW$Close)

tb <- table(X2330_TW$Close - X2330_TW$Open > 0)
pie(tb)

plot(X2330_TW$Date, X2330_TW$Close)
## Quantmod
#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('GOOG')
## '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).
## [1] "GOOG"
chartSeries(GOOG)

Lists
s1 <- c(50,60,70,80,72)
s2 <- c(50,30,20,40)
s3 <- c(80,90,85,88,91,87)
list(stud1 = s1, stud2 = s2, stud3 = s3)
## $stud1
## [1] 50 60 70 80 72
##
## $stud2
## [1] 50 30 20 40
##
## $stud3
## [1] 80 90 85 88 91 87
item <-list(thing="hat", size=8.25)
item
## $thing
## [1] "hat"
##
## $size
## [1] 8.25
item$size * 10
## [1] 82.5
test <-list(name="Toby", score =c(87,57,72))
test$name
## [1] "Toby"
test$score[2]
## [1] 57
li <- list(c(3,5,12), c(2,4,5,8,10))
li[[1]]
## [1] 3 5 12
li
## [[1]]
## [1] 3 5 12
##
## [[2]]
## [1] 2 4 5 8 10
li[[2]]
## [1] 2 4 5 8 10
li[[2]][5]
## [1] 10
s1 <- c(50,60,70,80,72)
s2 <- c(50,30,20,40)
s3 <- c(80,90,85,88,91,87)
test <- list(stud1 = s1, stud2 = s2, stud3 = s3)
mean(s3)
## [1] 86.83333
for(u in test){
print(mean(u))
}
## [1] 66.4
## [1] 35
## [1] 86.83333
lapply(test, function(u) mean(u))
## $stud1
## [1] 66.4
##
## $stud2
## [1] 35
##
## $stud3
## [1] 86.83333
lapply(test, function(u) u[1])
## $stud1
## [1] 50
##
## $stud2
## [1] 50
##
## $stud3
## [1] 80
lapply(test, function(u) u[length(u)])
## $stud1
## [1] 72
##
## $stud2
## [1] 40
##
## $stud3
## [1] 87
lapply(test, function(u) sum(u))
## $stud1
## [1] 332
##
## $stud2
## [1] 140
##
## $stud3
## [1] 521
li <- list(c(3,5,12), c(2,4,5,8,10))
lapply(li, function(e) sum(e))
## [[1]]
## [1] 20
##
## [[2]]
## [1] 29
lapply(li, sum)
## [[1]]
## [1] 20
##
## [[2]]
## [1] 29
Flow Control
x <- 2
if (x > 3){
print("x > 3 ")
}else{
print("x <= 3")
}
## [1] "x <= 3"
x <- 3
if (x > 3){
print("x > 3 ")
}else if (x == 3){
print("x == 3")
}else{
print("x < 3")
}
## [1] "x == 3"
BMI <- 28
if (BMI < 18.5){
print('體重過輕')
} else if(BMI >= 18.5 & BMI < 24 ){
print('正常範圍')
} else if(BMI >= 24 & BMI < 27 ){
print('過重')
} else if(BMI >= 27 & BMI < 30 ){
print('輕度肥胖')
} else if(BMI >= 30 & BMI < 35 ){
print('中度肥胖')
} else {
print('重度肥胖')
}
## [1] "輕度肥胖"
bmi_vec
## Brian Toby Sherry
## 22.53086 30.46112 14.36734
getStatus <- function(BMI){
if (BMI < 18.5){
return('體重過輕')
} else if(BMI >= 18.5 & BMI < 24 ){
return('正常範圍')
} else if(BMI >= 24 & BMI < 27 ){
return('過重')
} else if(BMI >= 27 & BMI < 30 ){
return('輕度肥胖')
} else if(BMI >= 30 & BMI < 35 ){
return('中度肥胖')
} else {
return('重度肥胖')
}
}
lapply(bmi_vec, function(bmi) getStatus(bmi))
## $Brian
## [1] "正常範圍"
##
## $Toby
## [1] "中度肥胖"
##
## $Sherry
## [1] "體重過輕"
sapply(bmi_vec, function(bmi) getStatus(bmi))
## Brian Toby Sherry
## "正常範圍" "中度肥胖" "體重過輕"
sapply(bmi_vec, getStatus)
## Brian Toby Sherry
## "正常範圍" "中度肥胖" "體重過輕"
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
for (qoo in 1:10){
print(qoo)
}
## [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")
x
## [1] "sunny" "rainy" "cloudy" "rainy" "cloudy"
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 (rec in x){
print(rec)
}
## [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
#http://www.appledaily.com.tw/realtimenews/section/new/6
url <- 'http://www.appledaily.com.tw/realtimenews/section/new/'
for(i in 1:5){
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
paste('Hello', 'World')
## [1] "Hello World"
?paste
paste('Hello', 'World', sep='')
## [1] "HelloWorld"
paste0('Hello', 'World')
## [1] "HelloWorld"
url <- 'http://www.appledaily.com.tw/realtimenews/section/new/'
for(i in 1:5){
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"
Function
# function 1
f <- function(){
'Hello World'
}
# function 2
f <- function(){
return('Hello World')
}
f()
## [1] "Hello World"
addNum <- function(a , b){
a + b
}
addNum(2,3)
## [1] 5
addNum2 <- function(a, b, c = 50){
a + b + c
}
addNum2(2,3)
## [1] 55
multiNum <- function(a, b, c = 3){
a + b * c
}
multiNum(3,5)
## [1] 18
multiNum(b=3,a=5)
## [1] 14
multiNum(b=3,a=5,2)
## [1] 11
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, 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, n=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(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
?head
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
seq(1,20, length.out = 2)
## [1] 1 20
seq(1,20, l = 2)
## [1] 1 20
f =function(a, b){
a * 2
}
f(3)
## [1] 6
f =function(a, b){
a + b
}
#f(3)
getwd()
## [1] "C:/Users/Administrator/Desktop"
getCommonWords <- function(filename){
article <- file(filename, "r")
content <- readLines(article)
seg_str <- strsplit(content, ' ')
tb <- table(unlist(seg_str))
w <- sort(tb, decreasing = TRUE)
#w[1:20]
w
}
#getCommonWords('cnn.txt')
#install.packages('wordcloud2')
library(wordcloud2)
wordcloud2(getCommonWords('cnn.txt'))
## Warning in readLines(article): 於 'cnn.txt' 找到不完整的最後一列
# use table to stat word frequency
a <- c(1,2,2,1,2)
table(a)
## a
## 1 2
## 2 3
# strsplit
a <- 'hello how are you i am fine'
strsplit(a, ' ')
## [[1]]
## [1] "hello" "how" "are" "you" "i" "am" "fine"
# unlist
a <- list(c(1,2,3), c(2,3,4,5,6,7))
a
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] 2 3 4 5 6 7
unlist(a)
## [1] 1 2 3 2 3 4 5 6 7