Created
July 4, 2014 04:17
-
-
Save zerodie/f4d81ebdb81224256844 to your computer and use it in GitHub Desktop.
r basic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) # 清空所有obj | |
x<-"R is easy to learn!" | |
y<-3 | |
y<-pi | |
bol1 <- T | |
bol2 <- TRUE | |
bol3 <- F | |
bol1 == bol2 | |
bol1 & bol2 | |
bol3 | 4 > 5 | |
4>2 | |
1>=2 | |
"Chia"=="Chia" | |
"Chia"=="chia" | |
a<-NA | |
a==NA # 注意:不能用==,要用 is.na(a) 才會回傳 TRUE。(另外還有 is.nan) | |
# NA: not available, NAN: not a number | |
is.na(a) | |
# if/else | |
value <- 5 | |
if (value > 4) { | |
print("The value is greater than 4") | |
} else if (2 < value & value <= 4) { | |
print("The value is greater than 2 and no greater than 4.") | |
} else { | |
print("The value is no greater than 2.") | |
} | |
# for loop | |
final_result <- 0 | |
for (i in 1:10) { | |
final_result <- final_result + i | |
} | |
# while loop | |
while (T){ | |
handsome <- readline('Are you handsome? ') | |
if (handsome == 'yes'){ | |
print('Really....!?') | |
} else { | |
print('Now we are talking.') | |
break | |
} | |
} | |
(final_result) | |
############ | |
# Vector # | |
############ | |
# c()為 R 中建立向量的內建函式 | |
# c(): concatenation function | |
# vector 中所有元素都必須是同一種資料屬性。 | |
vec1 <- c(1, 2, 3) | |
vec2 <- c("a", "b", "c") | |
# 字串優先於數字, 數字優先於布林值 | |
mix_vec1 <- c("a", 2) | |
# [1] "a" "2" | |
mix_vec2 <- c(2, T) | |
# [1] 2 1 | |
# Named Vector: | |
Bob <- c(age = 27, height = 187, weight = 80) | |
Bob | |
## age height weight | |
## 27 187 80 | |
vec1 <- c(1, 2, 3) | |
vec2 <- c(4, 5, 6) | |
vec1 + vec2 | |
## [1] 5 7 9 | |
vec1 - vec2 | |
## [1] -3 -3 -3 | |
vec1 * vec2 | |
## [1] 4 10 18 | |
vec1[1] + vec2[3] | |
## [1] 7 | |
names(Bob) | |
## [1] "age" "height" "weight" | |
Bob["age"] # reference by name. | |
############ | |
# Matrix # | |
############ | |
# 語法: matrix(elements, norw, ncol, byrow = F) | |
My_matrix1 <- matrix(1:6, 2, 3) | |
My_matrix2 <- matrix(1:6, 2, 3, byrow = T) # 先從row開始填 | |
My_matrix1 | |
## [,1] [,2] [,3] | |
## [1,] 1 3 5 | |
## [2,] 2 4 6 | |
My_matrix2 | |
## [,1] [,2] [,3] | |
## [1,] 1 2 3 | |
## [2,] 4 5 6 | |
My_matrix1 + My_matrix2 | |
## [,1] [,2] [,3] | |
## [1,] 2 5 8 | |
## [2,] 6 9 12 | |
My_matrix1 * My_matrix2 # matlab裡面的.* | |
## [,1] [,2] [,3] | |
## [1,] 1 6 15 | |
## [2,] 8 20 36 | |
# t(): Transpose | |
vec <- c(1:3) | |
(t(vec)) | |
## [,1] [,2] [,3] | |
## [1,] 1 2 3 | |
# 如果是vector,雖然看起來像是row matrix,但R就會先原封不動轉成matrix, | |
# 所以要變成column matrix就要轉兩次 | |
t(t(vec)) | |
# [,1] | |
# [1,] 1 | |
# [2,] 2 | |
# [3,] 3 | |
# %*%: Matrix Mulplication 矩陣乘法 | |
my_vec <- matrix(1:3, ncol = 1) # 用 c() 也跑得動。 | |
My_matrix1 %*% my_vec | |
# My_matrix1 %*% t(my_vec) | |
## Error: 非調和引數 | |
My_matrix1[1, c(1, 3)] # row 1 的col 1, 3 | |
## [1] 1 5 | |
My_matrix2[, c(2, 3)] # 所有row的col 2, 3 | |
## [,1] [,2] | |
## [1,] 2 3 | |
## [2,] 5 6 | |
dim(My_matrix1) # 2乘3 | |
## [1] 2 3 | |
########################### | |
# Factor and Data Frame # | |
########################### | |
# R 中有很多內建資料庫,其中包括你不可以不知道的 iris 資料庫。 | |
data(iris) | |
head(iris) # 前6筆 | |
## 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, n = 10) # 顯示 iris 前 10 筆資料 (預設為 6 筆) | |
tail(iris, n = 10) # 顯示 iris 後 10 筆資料 (預設為 6 筆) | |
Species <- iris[, "Species"] | |
class(Species) # R 會告訴你他是個 factor。 | |
Species2 <- as.numeric(Species) # 直接把 factor 轉成 numeric 向量。 | |
Species2 | |
# 你覺得上面這行 code 會跑出什麼呢? 試試看吧! | |
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | |
## [36] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 | |
## [71] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 | |
## [106] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 | |
## [141] 3 3 3 3 3 3 3 3 3 3 | |
# 用數字記下 | |
View(iris) | |
nrow(iris) # 顯示 iris 的列數 | |
## [1] 150 | |
ncol(iris) # 顯示 iris 的行數 | |
## [1] 5 | |
dim(iris) # 顯示 iris 的行、列數 | |
## [1] 150 5 | |
names(iris) #顯示 iris 的欄位名稱 | |
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" | |
## [5] "Species" | |
ind1 <- which(iris[, "Sepal.Length"] >= 6.5 & iris[, "Species"] == "virginica") | |
class(ind1) | |
## [1] "integer" | |
iris1 <- iris[ind1, ] | |
head(iris1) | |
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species | |
## 103 7.1 3.0 5.9 2.1 virginica | |
## 105 6.5 3.0 5.8 2.2 virginica | |
## 106 7.6 3.0 6.6 2.1 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 | |
ind2 <- which(iris[, "Sepal.Length"] < 5.8 & iris[, "Species"] == "setosa") | |
iris2 <- iris[ind2, ] | |
head(iris2) | |
## 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 | |
iris3 <- rbind(iris1, iris2) # 把兩張table上下疊在一起 | |
head(iris3) | |
iris4 <- cbind(iris1[1:10, ], iris2[1:10, ]) | |
head(iris4) # View(iris4) | |
sort(iris[1:30, 2]) | |
ind5 <- order(iris[, "Sepal.Length"], iris[, "Petal.Length"]) # 先對sepal.length小到大排序, 當其相等, 就用petal.length排, 回傳index | |
ind5[1:20] | |
# [1] 14 39 43 9 42 23 7 48 4 3 30 13 46 12 31 25 2 38 10 35 | |
class(ind5) | |
# [1] "integer" | |
iris5 <- iris[ind5, ] | |
head(iris5) | |
# 改變欄位名字 | |
iris6 <- iris | |
colnames(iris6) <- c("SLength", "SWidth", "PLength", "PWidth", "Sp") | |
# 也可以用 names(iris6) <- c('SLength', 'SWidth', 'PLength', 'PWidth', 'Sp') | |
head(iris6) | |
## SLength SWidth PLength PWidth Sp | |
## 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 | |
########## | |
# List # | |
########## | |
# list 是非常方便好用的資料形態。尤其是需儲存不同類型資料的時候,特別好用。 | |
list(1,'2') | |
list(1, T) | |
Bob <- list(age = 27, weight = 80, favorite_data_name = "iris", favorite_data = iris) # 可以塞各種 | |
(age1 <- Bob[1]) # 用一個[]會回傳整個list | |
## $age | |
## [1] 27 | |
class(age1) | |
## [1] "list" | |
(age2 <- Bob[[1]]) # 只回傳值 | |
## [1] 27 | |
class(age2) | |
## [1] "numeric" | |
(Age_and_DataName <- Bob[c(1, 3)]) # 回傳list | |
## $age | |
## [1] 27 | |
## | |
## $favorite_data_name | |
## [1] "iris" | |
head(Bob[["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 | |
my_sum <- function(lst) { | |
final_result <- 0 | |
for (num in lst) { | |
final_result <- final_result + num | |
} | |
return(final_result) | |
} | |
numbers <- c(1, 2, 5, 6, 8, 33) | |
my_sum(numbers) | |
## [1] 55 | |
# Global v.s. Local | |
x <- 5 | |
my_fun <- function() { | |
x <- 6 | |
return(x + 1) | |
} | |
my_fun() # 7 | |
print(x) # 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment