# R語言學習路程
- 學習影片:[5分鐘R語言系列](https://www.youtube.com/playlist?list=PLghWJf3e_h2GMPHNGbT4TN-kdfEvQgmPu)
- Question:
1. [第十二集: 資料輸入輸出 之 資料進得去 分析才出得來](https://hackmd.io/T3k405vvTfivEdKntggOVw?view#%E7%AC%AC%E5%8D%81%E4%BA%8C%E9%9B%86-%E8%B3%87%E6%96%99%E8%BC%B8%E5%85%A5%E8%BC%B8%E5%87%BA-%E4%B9%8B-%E8%B3%87%E6%96%99%E9%80%B2%E5%BE%97%E5%8E%BB-%E5%88%86%E6%9E%90%E6%89%8D%E5%87%BA%E5%BE%97%E4%BE%86-0819-Question)
2. [第十七集: 做圖(下)神套件 ggplot2 之 下半部](https://hackmd.io/T3k405vvTfivEdKntggOVw?view#%E4%BD%BF%E7%94%A8gridExtra%E5%A5%97%E4%BB%B6%E5%88%87%E5%89%B2%E7%95%AB%E5%B8%83%EF%BC%9Agridarrangeg1g2g3g4nrow2ncol2-Question)
- 學習成果: https://drive.google.com/drive/folders/1Uc_VghUKButF4EEqYbdllQP-4lmgzGAb?usp=sharing
## 第一集: 安裝下載及簡介(0808)
### 安裝及下載R
- [R](https://cran.csie.ntu.edu.tw/bin/windows/base/R-4.2.1-win.exe)
- [RStudio](https://download1.rstudio.org/desktop/windows/RStudio-2022.07.1-554.exe)
- 命令執行欄

### 指派
- Note:若變數性質為文字,需於文字前後加上"
如:"(文字)"
- EX: a $=$ 5 或 a $<$ 5

### remove()
- 搜尋函數意思 $\Longrightarrow$ 前方加上問號
- EX:remove(a)

### getwd()
- 用以尋找目前的工作目錄

### setwd()
- 用以更改目前的工作目錄位址
- EX:setwd ( "C:/Users/" )

## 第二集: 認識向量、判斷類型、創建向量(0808)
### 六種向量類型: 數值、整數、邏輯、文字、日期、日期時間
### 判斷向量類型: class() ; is.向量類型()

### 創建向量: c ( ) ; rep ( 物件 , 重複次數 ) ; seq ( 從 , 到 , 間隔 )

## 第三集: 六種向量(0808)
### 數值(numeric)

### 整數(integer)
- 加個大L

### 數值和整數運算子
- 加 ( + ) 、 減 ( - ) 、 乘 ( * ) 、除 ( / )
- 商數 ( %/% ) 、 餘數 ( %% )

### 文字(character)
- 引號使用注意

### 邏輯(logical)
- 判斷條件

### 日期(Date)
- Sys.Date(),可轉數值

### 日期時間(POSIXct)
- Sys.time(),可轉數值

## 第四集: 中括號就是你大哥 (0811)
### 向量是單一類型
- 混和不同類型的向量會是什麼類型?

### 判斷向量類型
- class()
- is.向量類型()
- 可參考:
https://hackmd.io/T3k405vvTfivEdKntggOVw#%E7%AC%AC%E4%BA%8C%E9%9B%86-%E8%AA%8D%E8%AD%98%E5%90%91%E9%87%8F%E3%80%81%E5%88%A4%E6%96%B7%E9%A1%9E%E5%9E%8B%E3%80%81%E5%89%B5%E5%BB%BA%E5%90%91%E9%87%8F0808
- inherits(物件,”向量類型”)

### 轉換向量類型
- as.向量類型()

### 向量的索引 切割 篩選
- [] 中括號哥

## 第五集: 神秘的流程控制 還搞不懂if else,for,while ?? (0811)
### 流程控制基本上就是if else; for; while
### if else家族 :
- if(){}

- if(){} else{}

- if(){} else if (){} else if(){} else{}

### for(x in 向量){....}

## 第六集: 較複雜資料結構(上) 之 清單就是超級包包 (0811)
### while(條件判斷){...}
- for 用在你已經知道要重複幾次, while 則否

### 較複雜的資料結構:
- 清單list
- 因素factor
- 資料框dataframe
- 矩陣matrix
- 陣列array
### 清單list()是超級包包
- [[]] 可用以索引 篩選

- 還可以用 $ 號

## 第七集: 較複雜資料結構(中) 之 Data frame 終於出來了 (0811)
### factor (向量,ordered=T,levels=c(...))

### data.frame(a,b,c)
- 列是橫的 行是直的

### str( )可以看data frame的結構

### matrix(向量,nrow= , byrow= )

### data frame和matrix都可以用中括號來索引 [列,行]
## 第八集: 較複雜資料結構(下) 之 Data frame學得好 R語言沒煩惱 (0811)
### Array是多維向量
- array(向量,dim = c(...))

### data frame基本操作:
- Note:
這個R內建的鳶尾花(iris)資料集是非常著名的生物資訊資料集之一,取自美國加州大學歐文分校的機械學習資料庫(http://archive.ics.uci.edu/ml/datasets/Iris),資料的筆數為150筆,共有五個欄位:
1. 花萼長度(Sepal Length):計算單位是公分。
2. 花萼寬度(Sepal Width):計算單位是公分。
3. 花瓣長度(Petal Length) :計算單位是公分。
4. 花瓣寬度(Petal Width):計算單位是公分。
5. 類別(Class):可分為Setosa,Versicolor和Virginica三個品種。
(參考資料:https://www.cc.ntu.edu.tw/chinese/epaper/0031/20141220_3105.html)
- dim( )
- head( )
- tail( )
- colnames( )
- summary( )

- 中括號裡可以放條件判斷

### data frame
- 直接用$加變數

- 指派為NULL消除變數

### rbind( )上下合併data frame
## 第九集: 資料框操作系列 (上) 之 dplyr帶你飛 (0813)
### install.packages (”包裹名”)、library(包裹名)
```R=+
install.packages("dplyr") ##安裝
library(dplyr) ##載入
```
### dplyr : filter 、 select 、 mutate 、 arrange 、 summarise
### filter (資料框,條件) 或用 [條件, ]
- 功能:篩選條件用
```R=+
filter(iris,iris$Sepal.Length > 6.5)
```

```R=+
iris[iris$Sepal.Length > 6.5,]
```

### select (資料框,變數名) 或用 [ ,變數名]
- 功能:列出單項數據內容
```R=+
select(iris,Sepal.Length)
```

```R=+
iris[ ,"Sepal.Length"]
```

```R=+
iris[ ,"Sepal.Length" , drop = F ]
```

### mutate (資料框,新變數) 或用 $號
- 功能:選取一定範圍資料
```R=+
mutate(iris,abc = 1:150)
```

### arrange (資料框,變數名)
- 功能:將其中一項數據做大小排列
1. 遞增排序
```R=+
arrange(iris,Sepal.Length)
```

2. 遞減排序
```R=+
arrange(iris,desc(Sepal.Length))
```

### summarise (資料框,描述統計)
- 功能:同時取得描述統計資料
```R=+
summarise( iris , mean(Sepal.Length) , sd(Sepal.Length) )
```

## 第十集: 資料框操作系列 (中) 之 tidyr轉起來 (0816)
### dplyr : group_by 結合 summarise()
- 功能:容易分類資料框
group_by 結合 summarise()
%>%就是下一步
```R=+
iris %>%
group_by(Species)%>%
summarise(mean(Sepal.Length) , mean(Sepal.Width))
```

### tidyr : spread 及 gather 函數
- 長型 vs. 寬型
長型:一列一個數
寬型:一列兩個以上
```R=+
a = c("male","male","female","female")
b = c("black","white","black","white")
c = c(80,40,60,70)
df1 = data.frame(gender = a ,color = b ,population = c)
```

- spread 目標資料框,key=””,value=””
```R=+
df2 = spread(df1 ,key = "color" ,value = "population")
```

- gather 目標資料框,key=””,value=””, keylevel
```R=+
df3 = gather(df2 ,key = "color" ,value = "population" ,black ,white)
```

## 第十一集: 資料框操作系列 (下) 之 資料框合體大法 (0816)
### rbind( df1,df2 )
- 數據設定
```R=+
a = iris[1:5, ]
b = iris[11:15, ]
a
b
```

```R=+
c = rbind(a,b)
c
```

### cbind( df1,df2 )
- 數據設定
```R=+
d = c[,1:3]
e = c[,4:5]
d
e
```

```R=+
f = cbind(d,e)
f
```

### merge( df1,df2,by.x=””,by.y=””,all.x=T,all.y=T )
- 數據設定
```R=+
a = c("爸爸","媽媽","姐姐")
b = c(88,99,77)
c = c("爸爸","姑姑","媽媽")
d = c(8,6,7)
df1 = data.frame(a,b)
df2 = data.frame(c,d)
df1
df2
```

```R=+
merge(df1,df2,by.x = "a",by.y = "c")
```
```R=+
merge(df1,df2,by.x = "a",by.y = "c",all.x = T,all.y = T)
```

## 第十二集: 資料輸入輸出 之 資料進得去 分析才出得來 (0819) **(Question!)**
### 輸入:
- 如何輸入.txt: read.table("路徑",header=T,stringsAsFactors=F)
- .tsv: read.table("路徑",header=T,stringsAsFactors=F)
- .csv: read.table("路徑",header=T,stringsAsFactors=F,sep=",") 或用 read.csv ("路徑",header=T,stringAsFactors=F)
- .xlsx: 安裝包裹readxl 使用read_excel ()
### 輸出:
- write.table ( 資料框,"路徑")
```R=+
a = c("爸爸","媽媽","姐姐")
b = c(88,99,77)
df1 = data.frame(a,b)
df1
```
## 第十三集: 自創函數(上)函數創起來
### 創建函數 :
1.
```R=+
abc= function (x){ #函數參數
x=2*(x+100); #執行內容
return(x) #回傳值
}
abc(100)
```

2.
```R=+
bcd= function (x,y){ #函數參數
x=2*(x+100); #執行內容
y=3*(y+200);
return(x/y) #回傳值
}
bcd(10,20)
```

3.
```R=+
cde= function (x,y,z = T){
x=2*(x+100);
y=3*(y+200);
if(z){
return(x/y);
}else{
return(y/x);
}
bcd(10,20)
```
### 全域變數 VS 區域變數
- 全域變數

Note:全域變數可以拿到函數區塊使用,但是存在於函數區塊中的區域變數並不能拉到函數區塊外使用
- 函數錯誤使用
```R=+
sqrt('aaaaa')
sqrt(-5)
```

## 第十四集: 自創函數(下)trycatch 及 apply家族介紹
### tryout{ {return(...)}, warning=function(w){return()}, error=function(e){return()} }
```R=+
new.sqrt = function(x){
tryCatch(
{ return(sqrt(x)) }, #回傳
warning = function(w){ #警示發生時回傳隻訊息
return("請不要輸入負數或0")},
error = function(e){ #錯誤發生時回傳之訊息
return("請不要輸入文字向量")}
)
}
```

### apply家族:
- 數據設定
```R=+
a = matrix(sample(1:50,20),ncol = 4)
b = data.frame(a = c(1,2,3),b = c(7,8,9))
c = list(a = sample(1:10,4),b = c(T,T,F,F))
a
b
c
```

- apply(矩陣或資料框, 1或2, 函數)
```R=+
apply(a,1,sum)
apply(a,2,sum)
apply(b,1,mean)
apply(b,2,sd)
```

- lapply(清單, 函數)
```R=+
lapply(c,sum)
```

- sapply(清單, 函數)
```R=+
sapply(c,sum)
```

```R=+
sapply(c,sum,simplify = F)
```

## 第十五集: 做圖(上)基礎繪圖系統
### 直方圖 hist( )
```R=+
hist(iris$Sepal.Length)
hist(iris$Sepal.Length,main = "IRIS的Sepal Length", ylab = "頻率", xlab = "Sepal Length", las = 1, col = "lightblue", border = "darkblue")
```

### 長條圖 barplot(table(...))
```R=+
barplot(table(iris$Species))
barplot(table(iris$Species), main = "IRIS的Species", xlab = "Species", las = 1)
barplot(table(iris$Species), main = "IRIS的Species", xlab = "Species", horiz = T, las = 1, cex.names = 1, cex.axis = 1.2)
```


### 盒鬚圖 boxplot(Y~X)
```R=+
boxplot(iris$Sepal.Length ~ iris$Species)
boxplot(iris$Sepal.Length ~ iris$Species,main = "各品種IRIS的Sepal Length", xlab = "品種", ylab = "Sepal Length", las = 1)
```


### 散布圖 plot(Y~X)
```R=+
plot(iris$Sepal.Length ~ iris$Sepal.Width, main = "Sepal length, width cor", xlab = "Sepal width", ylab = "Sepal length", las = 1, pch = 16, col = "red")
```


### 函數圖 curve(函數,from to)
```R=+
curve(x^2, from = 0, to = 100, las = 1)
```


### 歷年趨勢圖 plot(Y~X,type=”1”)
- 數據設定
```R=+
b = data.frame(a = 81:108, b = c(sample(100:120,7),sample(120:140,7),sample(140:160,7),sample(160:180,7)))
```
```R=+
plot(b$b~b$a,type = "l",las = 1)
```


### 重要參數 : main, xlab, ylab, las, border ,cex.names, cex.axis, pch, col
- main:圖的名稱
- xlab:x軸名稱
- ylab:y軸名稱
- las:y軸數值便正
- border:直方邊線顏色
- cex.names:字體大小
- cex.axis:字體大小
- pch:點的形狀
- col:直方顏色
- horiz:長條圖變水平
### 畫板切割 par(mfrow=c())
```R=+
par(mfrow=c(3,3))
hist(iris$Sepal.Length,main = "IRIS的Sepal Length", ylab = "頻率", xlab = "Sepal Length", las = 1, col = "lightblue", border = "darkblue")
boxplot(iris$Sepal.Length ~ iris$Species)
boxplot(iris$Sepal.Length ~ iris$Species,main = "各品種IRIS的Sepal Length", xlab = "品種", ylab = "Sepal Length", las = 1)
boxplot(iris$Sepal.Length ~ iris$Species,main = "各品種IRIS的Sepal Length", xlab = "品種", ylab = "Sepal Length", las = 1)
plot(iris$Sepal.Length ~ iris$Sepal.Width, main = "Sepal length, width cor", xlab = "Sepal width", ylab = "Sepal length", las = 1, pch = 16, col = "red")
curve(x^2, from = 0, to = 100, las = 1)
b = data.frame(a = 81:108, b = c(sample(100:120,7),sample(120:140,7),sample(140:160,7),sample(160:180,7)))
plot(b$b~b$a,type = "l",las = 1)
```


## 第十六集: 做圖(中)神套件 ggplot2 之 上半部
### 先安裝啟動ggplot2, dplyr (或其他具%>%的套件)
```R=+
install.packages("ggplot2")
library(ggplot2)
library(dplyr)
```
### 資料框 %大於% ggplot( aes(x= , y= ) ) + geom_圖類型( 相關參數設定 )
- ggplot2 直方圖
```R=+
iris %>%
ggplot(aes(x = Petal.Length)) +
geom_histogram(bins = 40)
iris %>%
ggplot(aes(x = Petal.Length)) +
geom_histogram(bins = 100)
```


- ggplot2 盒鬚圖
```R=+
iris %>%
ggplot(aes(x = Species, y = Petal.Length)) +
geom_boxplot()
```


- ggplot2 歷年變化線圖
- 數據設定
```R=+
b = data.frame(a = 81:108, b = c(sample(100:120,7),sample(120:140,7),sample(140:160,7),sample(160:180,7)))
```

- 程式碼
```R=+
b %>%
ggplot(aes(x = a, y = b)) +
geom_line()
```


- ggplot2 散布圖
```R=+
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point(shape = 6, color = "blue")
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point(aes(shape = Species, color = Species))
```


- ggplot2 長條圖
```R=+
iris %>%
ggplot(aes(x = Species)) +
geom_bar()
```


### 其他會用到的函數 : coord_flip
```R=+
iris %>%
ggplot(aes(x = Species)) +
geom_bar() +
coord_flip()
```


## 第十七集: 做圖(下)神套件 ggplot2 之 下半部
### 其他會用到的函數 :ggtitle, xlab, ylab
```R=+
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point() +
ggtitle("Petal 長寬散布圖") +
xlab("長") +
ylab("寬")
```


### 隱藏格線
- 隱藏主要格線:panel.grid.major = element_blank()
- 隱藏次要格線:panel.grid.minor = element_blank()
- 隱藏 X 軸主要格線:panel.grid.major.x = element_blank()
- 隱藏 Y 軸主要格線:panel.grid.major.y = element_blank()
- 隱藏 X 軸次要格線:panel.grid.minor.x = element_blank()
- 隱藏 Y 軸次要格線:panel.grid.minor.y = element_blank()
```R=+
iris %>%
ggplot(aes(x = Petal.Length)) +
geom_histogram(bins = 30) +
theme(panel.grid.major = element_blank())
```


### 使用gridExtra套件切割畫布:grid.arrange(g1,g2,g3,g4,nrow=2,ncol=2) **(Question!)**
- 安裝
```R=+
install.packages("gridExtra")
library(gridExtra)
```
- 程式碼
```R=+
grid.arrange(g1, g2, g3, g4, nrow = 2, ncol = 2)
```