Dboy
Taiwan UseR Group for Hackers
It is cool to be a hacker!!
圖片來源
Work this cool picture out.
Work this cool picture out.
And you can claim that you can do sketch by a computer!
接下來的系列課程:
接下來的系列課程:
接下來的系列課程:
接下來的系列課程:
接下來的系列課程:
在今天的課程裡也會讓大家體驗一下每個課程的主題是什麼。
For the best tutorial experience:
library("DSC2014Tutorial")
以資料屬性來分:
以資料形態來分:
(x <- 'R is easy to learn!') # 這是字串
(y <- 3) # 這是整數
(z <- pi) # 圓周率
## [1] "R is easy to learn!"
## [1] 3
## [1] 3.142
(k <- 1 + 2i) # 複數
(boo1 <- TRUE) # TRUE (or T for short)
(boo2 <- FALSE) # FALSE (or F for short)
## [1] 1+2i
## [1] TRUE
## [1] FALSE
bol1 <- T; bol2 <- TRUE
bol3 <- F
('Dboy' == 'Dboy')
[1] TRUE
(bol1 == bol2)
[1] TRUE
(bol1 & bol2)
[1] TRUE
(bol3 | 4 > 5)
[1] FALSE
4 > 2
[1] TRUE
1 >= 2
[1] FALSE
a <- NA
a == NA # 要用is.na(a)才會傳回TRUE或FALSE。(另外還有is.nan())
[1] NA
is.na(a)
[1] TRUE
猜猜看答案會是多少? (sum 是 R 中的內建函式,用以求和。)
my_vec <- c(1, 2, 5, 90, 37)
ind <- my_vec >= 5
sum(ind)
(sum(c(T, F, T)))
## [1] 2
my_vec <- c(1, 2, 5, 90, 37)
ind <- my_vec >= 5
sum(ind)
## [1] 3
c(): concatenation function
範例:
vec1 <- c(1, 2, 3)
vec2 <- c('a', 'b', 'c')
vector 中所有元素都必須是同一種資料屬性。
Named Vector:
(Bob <- c(age = 27, height = 187, weight = 80))
## age height weight
## 27 187 80
mix_vec1 <- c('a', 2)
mix_vec2 <- c(2, T)
猜看看結果會如何?
mix_vec1 <- c('a', 2)
mix_vec2 <- c(2, T)
猜看看結果會如何?
Why?
vec <- c(4, 5, 6, 11, 5)
length(vec)
Bob
names(Bob)
## [1] 5
## age height weight
## 27 187 80
## [1] "age" "height" "weight"
c() 也可以被用來結合兩個向量。
x <- c(1:5)
y <- c(2, 4, 8)
z <- c(x, y)
z
## [1] 1 2 3 4 5 2 4 8
定義一個向量 me 記錄自己的身高(公分)、體重(公斤)與年齡。
定義一個向量 me 記錄自己的身高(公分)、體重(公斤)與年齡。
女性參考答案: me <- c(age = "18 forever", W = "secret", height="非常高佻")
定義一個向量 me 記錄自己的身高(公分)、體重(公斤)與年齡。
如果我還想記錄頭髮的顏色跟電話號碼呢?
list 是非常方便好用的資料形態。尤其是需儲存不同類型資料的時候,特別好用。
還記得剛剛提過的優先順序嗎?
比較:
data(iris)
Bob <- list(age=27, weight = 80,
favorite_data_name = 'iris', favorite_data = iris)
Syntax: vec[index]
Examples:
vec <- c(1, 5, 10, 33, 6)
vec[3]
vec[length(vec)]
## [1] 10
## [1] 6
Syntax: vec["name"]
Dboy <- c(age=27, weight=82, height=172)
Dboy["age"]
## age
## 27
Syntax: a_list[index] or a_list[[index]]
Examples:
Bob[1]; class(Bob[1])
Bob[[1]]; class(Bob[[1]])
## $age
## [1] 27
## [1] "list"
## [1] 27
## [1] "numeric"
Syntax: a_list["name"] or a_list[["name"]]
Examples:
Bob["age"]
Bob[["age"]]
## $age
## [1] 27
## [1] 27
A Matrix is something looks like this:
A Matrix has two dimensions, denoted by i and j.
i for row indexing, j for column indexing.
i alone can specify one row.
j alone can specify one column.
i together with j can specify one element in a matrix.
Syntax: matrix(elements, nrow, ncol, byrow = FALSE)
M1 <- matrix(c(1:144), 12, 12)
Syntax: my_matrix[i, ] or my_matrix[, j]
M1[6, ]
## [1] 6 18 30 42 54 66 78 90 102 114 126 138
M1[, 6]
## [1] 61 62 63 64 65 66 67 68 69 70 71 72
colnames(M1) <- LETTERS[1:12]
M1[, 'F']
## [1] 61 62 63 64 65 66 67 68 69 70 71 72
What will happend to matrix Mat?
Mat <- matrix(c(1:15), 3, 5)
Mat[3, 3] <- "Ha Ha!" # t(Mat)
Mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] "1" "4" "7" "10" "13"
## [2,] "2" "5" "8" "11" "14"
## [3,] "3" "6" "Ha Ha!" "12" "15"
data(iris); iris <- iris[1:12, ];iris[6, ]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 6 5.4 3.9 1.7 0.4 setosa
iris[, 2]
## [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4
iris[, "Sepal.Width"]
## [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4
colnames(iris) <- c("Sepal.L", "Sepal.W", "Petal.L", "Petal.W", "Species")
iris
## Sepal.L Sepal.W Petal.L Petal.W 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
## 11 5.4 3.7 1.5 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
my_list <- list(Age = c(17, 22, 38), B.Type = c("A", "B", "O"))
(my_data <- as.data.frame(my_list))
str(my_data)
## Age B.Type
## 1 17 A
## 2 22 B
## 3 38 O
## 'data.frame': 3 obs. of 2 variables:
## $ Age : num 17 22 38
## $ B.Type: Factor w/ 3 levels "A","B","O": 1 2 3
In R, we use c() to specify multiple indices.
Example:
Dboy <- c(age=27, weight=82, height=172)
(Dboy)
(Dboy[c(1, 3)])
## age weight height
## 27 82 172
## age height
## 27 172
Examples:
data(cars)
speed <- cars[, "speed"]
speed[speed > 5]
## [1] 7 7 8 9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15
## [24] 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24 24 24
## [47] 24 25
Syntax: which(expression)
Examples:
my_vec <- runif(30, 0, 1) # 用 runif 從(0, 1)均勻分佈中抽取 30 個值。
(ind <- which(my_vec > 0.5))
(my_vec[ind])
## [1] 1 3 4 5 7 8 10 11 12 14 15 19 20 21 25 26 27
## [1] 0.7273 0.6269 0.6493 0.5838 0.7954 0.8832 0.7121 0.9923 0.7737 0.5421
## [11] 0.9414 0.6966 0.9282 0.8597 0.5213 0.7916 0.8398
Syntax: which(expression)
Examples:
(ind <- which(names(Dboy) %in% c("age", "weight")))
Dboy[ind]
## [1] 1 2
## age weight
## 27 82
Similarly, we use c() for multiple indexing in a list.
Syntax: my_list[c(ind1, ind2, ...)]
Example:
Bob[c(1, 3)]
## $age
## [1] 27
##
## $favorite_data_name
## [1] "iris"
Example:
(names(Bob))
(ind <- which(names(Bob) %in% c("age", "favorite_data")))
## [1] "age" "weight" "favorite_data_name"
## [4] "favorite_data"
## [1] 1 4
Example:
Bob[ind]
## $age
## [1] 27
##
## $favorite_data
## 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
## 11 5.4 3.7 1.5 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 14 4.3 3.0 1.1 0.1 setosa
## 15 5.8 4.0 1.2 0.2 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 34 5.5 4.2 1.4 0.2 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 39 4.4 3.0 1.3 0.2 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 43 4.4 3.2 1.3 0.2 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 49 5.3 3.7 1.5 0.2 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 51 7.0 3.2 4.7 1.4 versicolor
## 52 6.4 3.2 4.5 1.5 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 54 5.5 2.3 4.0 1.3 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 56 5.7 2.8 4.5 1.3 versicolor
## 57 6.3 3.3 4.7 1.6 versicolor
## 58 4.9 2.4 3.3 1.0 versicolor
## 59 6.6 2.9 4.6 1.3 versicolor
## 60 5.2 2.7 3.9 1.4 versicolor
## 61 5.0 2.0 3.5 1.0 versicolor
## 62 5.9 3.0 4.2 1.5 versicolor
## 63 6.0 2.2 4.0 1.0 versicolor
## 64 6.1 2.9 4.7 1.4 versicolor
## 65 5.6 2.9 3.6 1.3 versicolor
## 66 6.7 3.1 4.4 1.4 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 68 5.8 2.7 4.1 1.0 versicolor
## 69 6.2 2.2 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 77 6.8 2.8 4.8 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 85 5.4 3.0 4.5 1.5 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 99 5.1 2.5 3.0 1.1 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 105 6.5 3.0 5.8 2.2 virginica
## 106 7.6 3.0 6.6 2.1 virginica
## 107 4.9 2.5 4.5 1.7 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 109 6.7 2.5 5.8 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 112 6.4 2.7 5.3 1.9 virginica
## 113 6.8 3.0 5.5 2.1 virginica
## 114 5.7 2.5 5.0 2.0 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 119 7.7 2.6 6.9 2.3 virginica
## 120 6.0 2.2 5.0 1.5 virginica
## 121 6.9 3.2 5.7 2.3 virginica
## 122 5.6 2.8 4.9 2.0 virginica
## 123 7.7 2.8 6.7 2.0 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 127 6.2 2.8 4.8 1.8 virginica
## 128 6.1 3.0 4.9 1.8 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 131 7.4 2.8 6.1 1.9 virginica
## 132 7.9 3.8 6.4 2.0 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 136 7.7 3.0 6.1 2.3 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 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
Syntax: my_matrix[c(rowind1, rowind2, ...), c(colind1, colind2, ...)]
Example
M1[c(2, 4), 2:4]
## B C D
## [1,] 14 26 38
## [2,] 16 28 40
Syntax: my_matrix[c(rowind1, rowind2, ...), c(colind1, colind2, ...)]
Example:
Syntax: myDataFrame[c(rowind1, rowind2, ...), c(colind1, colind2, ...)]
Example:
data(iris); iris <- iris[1:12, ]
iris[c(2, 5), seq(from=1, to = 5, by = 2)]
## Sepal.Length Petal.Length Species
## 2 4.9 1.4 setosa
## 5 5.0 1.4 setosa
Syntax: myDataFrame[c(rowind1, rowind2, ...), c(colind1, colind2, ...)]
Example:
First Look:
Syntax: rbind(A, B) where A and B are two data frames or matrices
Again, let's play with iris data set.
Example:
data(iris)
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
Syntax: rbind(A, B) where A and B are two data frames or matrices
Again, let's play with iris data set.
Example:
iris[100:103, ]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
Syntax: rbind(A, B) where A and B are two data frames or matrices
Again, let's play with iris data set.
Example:
rbind(iris[1:3, ], iris[100:103, ])
## 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
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
First Look:
Syntax: cbind(A, B) where A and B are two data frames or matrices
Our beloved iris data set.
Example:
iris[1:5, 2:4]
## Sepal.Width Petal.Length Petal.Width
## 1 3.5 1.4 0.2
## 2 3.0 1.4 0.2
## 3 3.2 1.3 0.2
## 4 3.1 1.5 0.2
## 5 3.6 1.4 0.2
Syntax: cbind(A, B) where A and B are two data frames or matrices
Our beloved iris data set.
Example:
iris[101:105, 1:2]
## Sepal.Length Sepal.Width
## 101 6.3 3.3
## 102 5.8 2.7
## 103 7.1 3.0
## 104 6.3 2.9
## 105 6.5 3.0
Syntax: cbind(A, B) where A and B are two data frames or matrices
Our beloved iris data set.
Example:
cbind(iris[1:5, 2:4], iris[101:105, 1:2])
## Sepal.Width Petal.Length Petal.Width Sepal.Length Sepal.Width
## 1 3.5 1.4 0.2 6.3 3.3
## 2 3.0 1.4 0.2 5.8 2.7
## 3 3.2 1.3 0.2 7.1 3.0
## 4 3.1 1.5 0.2 6.3 2.9
## 5 3.6 1.4 0.2 6.5 3.0
還記得剛剛我們怎麼交新朋友的嗎?
Examples:
Sepal.Length <- iris[1:12, "Sepal.Length"]
(sort(Sepal.Length))
(order(Sepal.Length))
## [1] 4.4 4.6 4.6 4.7 4.8 4.9 4.9 5.0 5.0 5.1 5.4 5.4
## [1] 9 4 7 3 12 2 10 5 8 1 6 11
Examples:
ind <- order(iris[1:12, "Sepal.Length"], iris[1:12, "Sepal.Width"])
(iris_ordered <- iris[ind, ])
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 9 4.4 2.9 1.4 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 1 5.1 3.5 1.4 0.2 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
我們用房貸餘額資料來練習! (cl_info_other.csv)
之後會在 ETL 課程再度碰到它,也會學到進階的資料處理技巧。
# read.table 小技巧。
tmp <- read.table(DSC2014Tutorial::ETL_file('cl_info_other.csv'), sep = ',',
stringsAsFactors = F, header = T, nrows = 100)
colClasses <- sapply(tmp, class)
DF <- read.table(DSC2014Tutorial::ETL_file('cl_info_other.csv'), sep = ',',
header = T, colClasses = colClasses)
# read.table 小技巧。
tmp <- read.table(DSC2014Tutorial::ETL_file('cl_info_other.csv'), sep = ',',
stringsAsFactors = F, header = T, nrows = 100)
colClasses <- sapply(tmp, class)
DF <- read.table(DSC2014Tutorial::ETL_file('cl_info_other.csv'), sep = ',',
header = T, colClasses = colClasses)
學員OS: 這作業實在太 trivial 了,簡直侮辱我的智慧。
接下來的 ETL 課程保證會滿足你的渴望!
給定一個名叫 data 的 data frame (matrix)
names(data): 傳回 data 的所有欄位名稱。
nrow(data)/ncol(data): 傳回 data 的列 / 行數目。
dim(data)
head(data, n)/tail(data, n)/View(data)
(Petal.W <- as.factor(iris[1:12, "Petal.Width"]))
## [1] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 0.2 0.2
## Levels: 0.1 0.2 0.3 0.4
(Petal.W <- as.factor(iris[1:12, "Petal.Width"]))
## [1] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 0.2 0.2
## Levels: 0.1 0.2 0.3 0.4
有啥特別的? 不就多個 levels 嗎? 跟向量不是差不多?
(Petal.W <- as.factor(iris[1:12, "Petal.Width"]))
## [1] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 0.2 0.2
## Levels: 0.1 0.2 0.3 0.4
同款就不同師父啊(台)
Just try it!
Factor in R is a key-value mapping.
This is what you really get:
Petal.W
as.numeric(Petal.W)
## [1] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 0.2 0.2
## Levels: 0.1 0.2 0.3 0.4
## [1] 2 2 2 2 2 4 3 2 2 1 2 2
Vector in R
Factor in R: A Key-Value Mapping
Syntax: factor(x)
Syntax: factor(x)
Example:
test_factor <- c(1, 3, 3, 5, 2, 4, 2, 5)
test_factor <- factor(test_factor)
Syntax: factor(x)
Example:
test_factor
## [1] 1 3 3 5 2 4 2 5
## Levels: 1 2 3 4 5
Syntax: factor(x)
Example:
levels(test_factor) <- c("A", "B", "C", "D", "E")
test_factor
## [1] A C C E B D B E
## Levels: A B C D E
How can we correctly convert a factor into a vector?
Q: my_factor <- factor(seq(10, 1, -1))
1. my_vec <- seq(10, 1, by = -1)
my_vec[c(3, 3, 5, 1, 6)]
2. levels(my_factor): this will give you a vector of levels of a factor.
my_factor <- factor(seq(10, 1, -1))
Levels <- levels(my_factor)
my_vector <- Levels[as.numeric(my_factor)]
for (iterator){
#Do something here....
}
# 從 1 加到 10
final_result <- 0
for (i in 1:10){
final_result <- final_result + i
}
final_result
## [1] 55
# 讓 R 幫你驅邪避凶!!
# This is for Mac.
for (i in 1:5){
system("say 'Nann Moll Ah Mi Tow Fo'")
system("say 'Ah Men'")
}
# This is for Ubuntu.
for (i in 1:5){
system("espeak 'Nann Moll Ah Mi Tow Fo'")
system("espeak 'Ah Men'")
}
# This is for Windows.
for (i in 1:5){
system("espeak NannMollAhMiTowFo")
system("espeak AhMen")
}
Syntex:
if (condition_1){
#Do something here....
} else if (conditon_2){
#Do something here
} else {
#Do something here
}
Note: else if and else are optional.
Magic Vector:
c(1.45284450, -0.04625854, 0.5211828, -1.003045, -0.4641298)
Magic Vector:
c(1.45284450, -0.04625854, 0.5211828, -1.003045, -0.4641298)
(暫時)不要問我怎麼把這個向量生出來的。(汗)
Magic Vector:
c(1.45284450, -0.04625854, 0.5211828, -1.003045, -0.4641298)
或許你可以問助教,助教什麼都會!
One simple way to get the data if you're using our package.
data("RBasic_ForLoop_Ex")
其他更精彩的資料分析模型的理論與操作,敬請期待 Data Analysis 課程!
while (condition_1){
#Do something here....
}
while (T){
handsome <- readline('Are you handsome?[yes or no] ')
if (handsome == 'yes'){
print('Really....!?')
} else {
print('Now we are talking.')
break
}
}
All these operations are vectorized. (element-by-element operation)
Examples:
my_vec1 <- c(1, 3, 5, 7); my_vec2 <- c(2, 4, 6, 8)
(my_vec1 + my_vec2)
(my_vec1 * my_vec2)
## [1] 3 7 11 15
## [1] 2 12 30 56
Syntax: matrix1 %*% matrix2
Example:
set.seed(3690)
my_mat1 <- matrix(c(1:6), 2, 3)
my_mat2 <- matrix(runif(6), 3, 2)
(my_mat1 %*% my_mat2)
## [,1] [,2]
## [1,] 5.577 5.534
## [2,] 7.263 7.478
Syntax: solve(A, b)
Given a linear system like this:
\[
A x = b
\]
solve() will return:
\[ x^*= A^{-1} b \]
Examples:
(A <- matrix(runif(9), 3, 3))
(A_inv <- solve(A))
## [,1] [,2] [,3]
## [1,] 0.4278 0.95646 0.05677
## [2,] 0.8543 0.06763 0.81184
## [3,] 0.5929 0.76707 0.11537
## [,1] [,2] [,3]
## [1,] -4.458 -0.4842 5.601
## [2,] 2.775 0.1138 -2.166
## [3,] 4.460 1.7318 -5.713
Examples:
(A %*% A_inv)
## [,1] [,2] [,3]
## [1,] 1.00e+00 -4.163e-17 -6.106e-16
## [2,] 0.00e+00 1.000e+00 0.000e+00
## [3,] -2.22e-16 5.551e-17 1.000e+00
Examples:
b <- c(1, 2, 3)
A_inv_b <- solve(A, b)
A %*% A_inv_b
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
Examples:
b <- c(1, 2, 3)
A_inv_b <- solve(A, b)
A %*% A_inv_b
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
It's time for mini project!
起始點:
With 5% probability:
With 81% probability:
With 7% probability:
With 7% probability:
依此規則迭代出 40000 點,再把這些點畫成圖。
只要用我們有學過的 for/if 迴圈和矩陣運算就可以做到這件事。
你應該會看到:
可以把迭代出來的點用一個 data.frame 存起來。(例如說存成 coor )
最後用 plot(x = coor[, 2], y = coor[, 1], plt = c(0, 10, -5, 5), cex = 0.1, asp = 1) 把它畫出來。
這些參數不懂沒關係,它們的唯一功能就只是讓圖變漂亮而已。(很多我也是 Google 來的XD)
One simple way to open the template if you're using our package.
path <- DSC2014Tutorial::Basic_file("barnsley_fern_template.R")
utils::browseURL(path)
One simple way to open the template if you're using our package.
path <- DSC2014Tutorial::Basic_file("barnsley_fern_template.R")
utils::browseURL(path)
敬請期待 Data Visualization 教學課程。
Reference answer.
path <- DSC2014Tutorial::Basic_file("barnsley_fern_answer.R")
utils::browseURL(path)
Syntex:
my_function <- function(arg1, arg2 = arg2_default, ...){
# do something here
# return the result. (optional)
}
`%Q_Q%` <- function(x, y){
return(2*x + 5*y)
}
2 %Q_Q% 3
## [1] 19
`%(= ww =)%` <- function(x, y, z=3){
return(x + 2*y + z)
}
2 %(= ww =)% 3
## [1] 11
We use following example to demostrate how to set default values in function and the basic scoping rule in R.
MyFunction <- function(x, y, z=3, ...){
print("x, y, z:")
print(c(x=x, y=y, z=z))
print("The rest of args:")
print(c(...))
return(x + 2*y + 6*z + sum(...))
}
MyFunction(x=1, y=3) # It works without z!! (By "default", z = 3)
## [1] "x, y, z:"
## x y z
## 1 3 3
## [1] "The rest of args:"
## NULL
## [1] 25
MyFunction(1, 3, 5, 2, 9)
## [1] "x, y, z:"
## x y z
## 1 3 5
## [1] "The rest of args:"
## [1] 2 9
## [1] 48
MyFunction(1, 3, 5, 2, z = 9)
## [1] "x, y, z:"
## x y z
## 1 3 9
## [1] "The rest of args:"
## [1] 5 2
## [1] 68
MyFunction(1, 3, 5, y = 2, x = 9)
## [1] "x, y, z:"
## x y z
## 9 2 1
## [1] "The rest of args:"
## [1] 3 5
## [1] 27
MyFunction(1, 3, 5, y = 2, x = 9)
## [1] "x, y, z:"
## x y z
## 9 2 1
## [1] "The rest of args:"
## [1] 3 5
## [1] 27
It is time for Mini Project II!
接下來我們將一步步指導該如何造出這個 battleship()。
首先由電腦決定一個座標。
定義一個 list 變數 map 如下
map =list(c('O', 'O', 'O', 'O', 'O'),
c('O', 'O', 'O', 'O', 'O'),
c('O', 'O', 'O', 'O', 'O'),
c('O', 'O', 'O', 'O', 'O'),
c('O', 'O', 'O', 'O', 'O'))
用一個 for 迴圈把 map 中的每一個項目 print 出來。
定義一個變數 trial 並給予初始值 0 。(此變數將用於記錄玩家已經試過幾次)
用一個 while 迴圈來判斷 trial 是否超過可嘗試次數。如果沒有,更新 map 並顯示適當訊息。若已超過, break 當前迴圈。
One simple way to open the template file if you are using our package.
path <- DSC2014Tutorial::Basic_file("battleship_template.R")
## Error: there is no package called 'DSC2014Tutorial'
utils::browseURL(path)
readline('Are you a girl?') # readline() 會把輸入的資料存成字串。
sample.int(5, 1) # 從 1~5 中隨機抽取 1 個數字。
## [1] 4
print('I love R!'); cat('I love R!')
## [1] "I love R!"
## I love R!
path <- DSC2014Tutorial::Basic_file("battleship_answer.R")
## Error: there is no package called 'DSC2014Tutorial'
utils::browseURL(path)