kevin <- c(85,73)
marry <- c(72,64)
jerry <- c(59,66)
mat <- matrix(c(kevin, marry, jerry), nrow = 3, byrow=TRUE)
# [列,欄]
mat[,1] * 0.4 + mat[,2] * 0.6
## [1] 77.8 67.2 63.2
# 3 X 2
mat
## [,1] [,2]
## [1,] 85 73
## [2,] 72 64
## [3,] 59 66
# 2 X 1
weight <- matrix(c(0.4,0.6), nrow = 2)
# 3 X 2 * 2 X 1 = 3 X 1
mat %*% weight
## [,1]
## [1,] 77.8
## [2,] 67.2
## [3,] 63.2
Factor
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_category <- factor(temperature, order = TRUE, levels = c("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"
DataFrame
days <- c('mon','tue','wed','thu','fri')
temp <- c(22.2,21,23,24.3,25)
rain <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
class(days)
## [1] "character"
class(temp)
## [1] "numeric"
class(rain)
## [1] "logical"
weather <- matrix(c(days, temp, rain), nrow = 5)
weather
## [,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"
mean(weather[,2])
## Warning in mean.default(weather[, 2]): argument is not numeric or logical:
## returning NA
## [1] NA
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: chr "mon" "tue" "wed" "thu" ...
## $ temp: num 22.2 21 23 24.3 25
## $ rain: logi TRUE TRUE FALSE FALSE TRUE
summary(df)
## days temp rain
## Length:5 Min. :21.0 Mode :logical
## Class :character 1st Qu.:22.2 FALSE:2
## Mode :character Median :23.0 TRUE :3
## Mean :23.1
## 3rd Qu.:24.3
## Max. :25.0
#data()
data(iris)
class(iris)
## [1] "data.frame"
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
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
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
tail(iris, 10)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 141 6.7 3.1 5.6 2.4 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 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
1:3
## [1] 1 2 3
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[ c(1,3) , ]
## 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
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:3,c("Sepal.Length", "Sepal.Width") ]
## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
iris[1:3,1:2]
## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
#iris$Sepal.Length
# SELECT * FROM iris WHERE Species = 'setosa'
setosa.data <- iris[iris$Species == '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 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#iris[which(iris$Species == 'setosa'), ]
v <- c(50,60,70)
v[c(1,3)]
## [1] 50 70
v[c(TRUE, FALSE, TRUE)]
## [1] 50 70
sort(iris$Sepal.Length)
## [1] 4.3 4.4 4.4 4.4 4.5 4.6 4.6 4.6 4.6 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 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 5.2 5.3 5.4 5.4 5.4 5.4 5.4 5.4 5.5 5.5
## [55] 5.5 5.5 5.5 5.5 5.5 5.6 5.6 5.6 5.6 5.6 5.6 5.7 5.7 5.7 5.7 5.7 5.7 5.7
## [73] 5.7 5.8 5.8 5.8 5.8 5.8 5.8 5.8 5.9 5.9 5.9 6.0 6.0 6.0 6.0 6.0 6.0 6.1
## [91] 6.1 6.1 6.1 6.1 6.1 6.2 6.2 6.2 6.2 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3
## [109] 6.4 6.4 6.4 6.4 6.4 6.4 6.4 6.5 6.5 6.5 6.5 6.5 6.6 6.6 6.7 6.7 6.7 6.7
## [127] 6.7 6.7 6.7 6.7 6.8 6.8 6.8 6.9 6.9 6.9 6.9 7.0 7.1 7.2 7.2 7.2 7.3 7.4
## [145] 7.6 7.7 7.7 7.7 7.7 7.9
sort(iris$Sepal.Length, decreasing = TRUE)
## [1] 7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 7.2 7.1 7.0 6.9 6.9 6.9 6.9 6.8
## [19] 6.8 6.8 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.7 6.6 6.6 6.5 6.5 6.5 6.5 6.5 6.4
## [37] 6.4 6.4 6.4 6.4 6.4 6.4 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.2 6.2 6.2
## [55] 6.2 6.1 6.1 6.1 6.1 6.1 6.1 6.0 6.0 6.0 6.0 6.0 6.0 5.9 5.9 5.9 5.8 5.8
## [73] 5.8 5.8 5.8 5.8 5.8 5.7 5.7 5.7 5.7 5.7 5.7 5.7 5.7 5.6 5.6 5.6 5.6 5.6
## [91] 5.6 5.5 5.5 5.5 5.5 5.5 5.5 5.5 5.4 5.4 5.4 5.4 5.4 5.4 5.3 5.2 5.2 5.2
## [109] 5.2 5.1 5.1 5.1 5.1 5.1 5.1 5.1 5.1 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 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
grades <- c(60,50,20,70,40,80)
sort(grades)
## [1] 20 40 50 60 70 80
sort(grades, decreasing = TRUE)
## [1] 80 70 60 50 40 20
g <- c(60,50,20,70,40,80)
order(g, decreasing = TRUE)
## [1] 6 4 1 2 5 3
order(g)
## [1] 3 5 2 1 4 6
g[order(g, decreasing = TRUE)]
## [1] 80 70 60 50 40 20
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
## 131 7.4 2.8 6.1 1.9 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 51 7.0 3.2 4.7 1.4 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 121 6.9 3.2 5.7 2.3 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 77 6.8 2.8 4.8 1.4 versicolor
## 113 6.8 3.0 5.5 2.1 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 66 6.7 3.1 4.4 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 109 6.7 2.5 5.8 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 141 6.7 3.1 5.6 2.4 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 59 6.6 2.9 4.6 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 105 6.5 3.0 5.8 2.2 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 52 6.4 3.2 4.5 1.5 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 112 6.4 2.7 5.3 1.9 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 57 6.3 3.3 4.7 1.6 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 69 6.2 2.2 4.5 1.5 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 127 6.2 2.8 4.8 1.8 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 64 6.1 2.9 4.7 1.4 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 128 6.1 3.0 4.9 1.8 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 63 6.0 2.2 4.0 1.0 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 120 6.0 2.2 5.0 1.5 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 62 5.9 3.0 4.2 1.5 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 150 5.9 3.0 5.1 1.8 virginica
## 15 5.8 4.0 1.2 0.2 setosa
## 68 5.8 2.7 4.1 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 102 5.8 2.7 5.1 1.9 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 16 5.7 4.4 1.5 0.4 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 56 5.7 2.8 4.5 1.3 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 114 5.7 2.5 5.0 2.0 virginica
## 65 5.6 2.9 3.6 1.3 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 122 5.6 2.8 4.9 2.0 virginica
## 34 5.5 4.2 1.4 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 54 5.5 2.3 4.0 1.3 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 6 5.4 3.9 1.7 0.4 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 85 5.4 3.0 4.5 1.5 versicolor
## 49 5.3 3.7 1.5 0.2 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 60 5.2 2.7 3.9 1.4 versicolor
## 1 5.1 3.5 1.4 0.2 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 99 5.1 2.5 3.0 1.1 versicolor
## 5 5.0 3.6 1.4 0.2 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 61 5.0 2.0 3.5 1.0 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 2 4.9 3.0 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 58 4.9 2.4 3.3 1.0 versicolor
## 107 4.9 2.5 4.5 1.7 virginica
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 42 4.5 2.3 1.3 0.3 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
## 14 4.3 3.0 1.1 0.1 setosa
#install.packages('outbreaks')
library(outbreaks)
data(dengue_fais_2011)
View(dengue_fais_2011)
head(dengue_fais_2011)
## onset_date nr value
## 1 2011-09-15 7 0
## 2 2011-09-22 14 0
## 3 2011-09-29 21 0
## 4 2011-10-06 28 0
## 5 2011-10-13 35 0
## 6 2011-10-20 42 0
tail(dengue_fais_2011)
## onset_date nr value
## 52 2012-01-10 124 0
## 53 2012-01-17 131 0
## 54 2012-01-24 138 0
## 55 2012-01-31 145 0
## 56 2012-02-07 152 0
## 57 2012-02-14 159 0
class(dengue_fais_2011)
## [1] "tbl_df" "tbl" "data.frame"
summary(dengue_fais_2011)
## onset_date nr value
## Min. :2011-09-15 Min. : 7 Min. : 0.000
## 1st Qu.:2011-11-16 1st Qu.: 69 1st Qu.: 0.000
## Median :2011-11-30 Median : 83 Median : 0.000
## Mean :2011-11-30 Mean : 83 Mean : 2.719
## 3rd Qu.:2011-12-14 3rd Qu.: 97 3rd Qu.: 2.000
## Max. :2012-02-14 Max. :159 Max. :19.000
head(dengue_fais_2011)
## onset_date nr value
## 1 2011-09-15 7 0
## 2 2011-09-22 14 0
## 3 2011-09-29 21 0
## 4 2011-10-06 28 0
## 5 2011-10-13 35 0
## 6 2011-10-20 42 0
tail(dengue_fais_2011)
## onset_date nr value
## 52 2012-01-10 124 0
## 53 2012-01-17 131 0
## 54 2012-01-24 138 0
## 55 2012-01-31 145 0
## 56 2012-02-07 152 0
## 57 2012-02-14 159 0
sum(dengue_fais_2011[dengue_fais_2011$onset_date < '2012-01-01', 'value'])
## [1] 155
sample_data <- dengue_fais_2011[dengue_fais_2011[,1] < '2012-01-01',]
sum(sample_data[,3])
## [1] 155
?plot
## Help on topic 'plot' was found in the following packages:
##
## Package Library
## graphics /Library/Frameworks/R.framework/Versions/4.0/Resources/library
## base /Library/Frameworks/R.framework/Resources/library
##
##
## Using the first match ...
plot(dengue_fais_2011$onset_date, dengue_fais_2011$value, type = 'l')

R Lists
phone <- list(thing="iphone X" , height=5.65, width=2.79 )
phone
## $thing
## [1] "iphone X"
##
## $height
## [1] 5.65
##
## $width
## [1] 2.79
phone$thing
## [1] "iphone X"
phone$width
## [1] 2.79
student <- list(name="Toby", score = c(87,57,72))
student$name
## [1] "Toby"
student$score
## [1] 87 57 72
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
student
## $name
## [1] "Toby"
##
## $score
## [1] 87 57 72
student[[1]]
## [1] "Toby"
student[[2]]
## [1] 87 57 72
# y = f(x)
sum(li[[1]])
## [1] 20
sum(li[[2]])
## [1] 29
# apply function to lists
lapply(li, sum)
## [[1]]
## [1] 20
##
## [[2]]
## [1] 29
Control Flow
x <- 2
if(x > 3){
print("x > 3")
}else{
print("x <= 3")
}
## [1] "x <= 3"
x <- 2
if(x > 3){
print("x > 3");
} else if(x ==3){
print("x == 3")
}else{
print("x < 3")
}
## [1] "x < 3"
for(qoolovesoop in 1:10){
print(qoolovesoop)
}
## [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
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(letter in x) {
print(letter)
}
## [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
tour_history <- list(
t1 = c('USA','JAPAN', 'HONGKONG'),
t2 = c('CHINA','JAPAN'),
t3 = c('CHINA','HONGKONG')
)
for(t in tour_history){
print(t)
}
## [1] "USA" "JAPAN" "HONGKONG"
## [1] "CHINA" "JAPAN"
## [1] "CHINA" "HONGKONG"
'CHINA' %in% c('USA','JAPAN', 'HONGKONG')
## [1] FALSE
match('USA', c('USA','JAPAN', 'HONGKONG'))
## [1] 1
any('USA' == c('USA','JAPAN', 'HONGKONG'))
## [1] TRUE
cnt <- 0
for(t in tour_history){
if('CHINA' %in% t){
cnt <- cnt + 1
}
}
cnt
## [1] 2
?lapply
lapply(tour_history, function(x) match('CHINA', x))
## $t1
## [1] NA
##
## $t2
## [1] 1
##
## $t3
## [1] 1
sapply(tour_history, function(x) match('CHINA', x))
## t1 t2 t3
## NA 1 1
R 函數 (Function)
f <- function(x){
x + 3
}
f(5)
## [1] 8
f <- function(a, b = 2, c = NULL) {
a + b + c
}
f(2,3,4)
## [1] 9
f(a =2, c = 5)
## [1] 9
#f(c = 5)
addNum <- function(a = 2, b = 3) {
s <- a + b
s
}
addNum(2,3)
## [1] 5
addNum(7,4)
## [1] 11
addNum(b = 4)
## [1] 6
addNum()
## [1] 5
f <- function(a, b) {
a * 2
}
f(2)
## [1] 4
f <- function(a, b) {
a+ b
}
#f(3)
multiNum <- function(a = 2, b = 3, c = 4){
a + b * c
}
multiNum()
## [1] 14
multiNum(5,6,7)
## [1] 47
multiNum(c=5,b=6,7)
## [1] 37
?sum
sum(5,6,7,8,9)
## [1] 35
sum(5,6,7,8,9,NA)
## [1] NA
sum(5,6,7,8,9,NA, na.rm = TRUE)
## [1] 35
matrix(c(2,3,4,5), nro = 2)
## [,1] [,2]
## [1,] 2 4
## [2,] 3 5
tour_history
## $t1
## [1] "USA" "JAPAN" "HONGKONG"
##
## $t2
## [1] "CHINA" "JAPAN"
##
## $t3
## [1] "CHINA" "HONGKONG"
lapply(tour_history, function(x) match('CHINA', x))
## $t1
## [1] NA
##
## $t2
## [1] 1
##
## $t3
## [1] 1
sapply(tour_history, function(x) match('CHINA', x))
## t1 t2 t3
## NA 1 1
stat_tour <- function(country){
cnt <- 0
for(t in tour_history){
if(country %in% t){
cnt <- cnt + 1
}
}
cnt
}
stat_tour('JAPAN')
## [1] 2
stat_tour('CHINA')
## [1] 2
stat_tour('USA')
## [1] 1
stat_tour2 <- function(x, country){
match(country, x)
}
lapply(tour_history, function(x) stat_tour2(x, country = 'CHINA'))
## $t1
## [1] NA
##
## $t2
## [1] 1
##
## $t3
## [1] 1