鏡像設置
1.菜單法 可以配置CRAN鏡像,一般選清華
*但是bioconductor上R包沒法用這個CRAN鏡像下載,而且無法保證一定從此CRAN下載R包。
options()$repos
options()$BioC_mirror
#可以通過此命令查找目前Rstudio通過什么鏡像下載R包
2.代碼法
*同時設置CRAN鏡像及bioconductor鏡像
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #清華源CRAN
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #中科大源bioconductor
*每次啟動都需要先運行。
3.高級模式 一勞永逸
R的兩個最重要的配置文件
.Renviron #設置R的環境變量用,此處按下不表
.Rprofile #R的代碼文件,啟動Rstudio時自動運行
在Linux中可以使用file.edit命令編輯
file.edit(~/.Rprofile) #注意此處是Linux命令
然后在出現的對話框中添加2的上述兩行option代碼,即可配置完成
安裝R包
#首先google一下想安的包在CRAN還是bioconductor, 再分別選擇使用以下命令
install.packages(“包”)
BiocManager::install(“包”)
加載
library(包)
require(包)
學習dplyr包
加載“dplyr"
此前已通過“tydiverse”安裝“dplyr”,此處直接加載
library("dplyr")
學一下"dplyr"
help(“dplyr")
browseVignettes(package = "dplyr")
#找到dplyr的網絡介紹鏈接:[Introduction to dplyr](http://127.0.0.1:19586/library/dplyr/doc/dplyr.html),然后chrome有道網頁翻譯2.0打開右側窗口顯示,左邊敲Rscript,不能太方便。
單表數據操作:都是“動詞(表格,命令)”格式
- filter() #按行篩選
- arrange() #按列排序
- select() #按列篩選
- mutate() transmute() #新增列,該列是現有列的函數
- summarise() #匯總
- sample_n() sample_frac() #隨機抽樣
- slice() #按等級秩序選擇各行,少用,可用filter(), 或row_number()代替
數據:使用生信星球教學數據集
> test <- iris[c(1:2,51:52,101:102),] #把內置數據框iris的相應行提取出來賦值給test數據框
> test
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
51 7.0 3.2 4.7 1.4 versicolor
52 6.4 3.2 4.5 1.5 versicolor
101 6.3 3.3 6.0 2.5 virginica
102 5.8 2.7 5.1 1.9 virginica
* filter() #按行篩選
> filter(test, Species == "setosa")
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
#等同于,
test[test$Species == "setosa",]
> filter(test, Species == "setosa"&Sepal.Length > 5 )
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
#等同于,
test[test$Species == "setosa" & test$Sepal.Length > 5,]
> filter(test, Species %in% c("setosa","versicolor"))
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 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
#等同于,
test[test$Species %in% c("setosa","versicolor"),]
* arrange() #按列排序
> arrange(test, Sepal.Length) #默認從小到大排序
> arrange(test, desc(Sepal.Length)) #用desc從大到小
> arrange(test, Petal.Length, desc(Sepal.Length)) #先按變量1從小到大,再按變量2從大到小
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 6.4 3.2 4.5 1.5 versicolor
4 7.0 3.2 4.7 1.4 versicolor
5 5.8 2.7 5.1 1.9 virginica
6 6.3 3.3 6.0 2.5 virginica
*select() #按列篩選
#具體用法,
select(test,1:3) #也可以select(test,Sepal.Length:Petal.Length)
select(test,c(1,5))
select(test,Sepal.Length)
select(test,- c(1,5)) # -號可做反選
#select() 可以選擇并同時重命名變量,但不如rename()好用
#可搭配starts_with(),ends_with(),matches()和contains()使用風味更佳 具體可[?select](R Documentation
:Select/rename variables by name)看Examples
* mutate() transmute() #新增列,該列是 現有列的函數
#注意允許引用剛剛創建的列來新建列,例如,
mutate(test, new = Sepal.Length * Sepal.Width, new100 = new * 100)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new new100
1 5.1 3.5 1.4 0.2 setosa 17.85 1785
2 4.9 3.0 1.4 0.2 setosa 14.70 1470
3 7.0 3.2 4.7 1.4 versicolor 22.40 2240
4 6.4 3.2 4.5 1.5 versicolor 20.48 2048
5 6.3 3.3 6.0 2.5 virginica 20.79 2079
6 5.8 2.7 5.1 1.9 virginica 15.66 1566
# 假如只想保存新變量,用transmute()
> transmute(test, new = Sepal.Length * Sepal.Width, new100 = new * 100)
new new100
1 17.85 1785
2 14.70 1470
3 22.40 2240
4 20.48 2048
5 20.79 2079
6 15.66 1566
* summarise() #匯總, 可結合group_by()使用
# 計算Sepal.Length的平均值和標準差
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
# 先按照Species分組,計算每組Sepal.Length的平均值和標準差
group_by(test, Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
#summarise描述統計[?summarise](Reduce multiple values down to a single value)
* sample_n() sample_frac() #隨機抽樣
#隨機抽取n個樣本或幾個比例frac樣本
> sample_n(test,2)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 6.3 3.3 6.0 2.5 virginica
2 4.9 3.0 1.4 0.2 setosa
> sample_frac(test,1/6)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7 3.2 4.7 1.4 versicolor
dplyr支持
1.管道操作 %>% (cmd/ctr + shift + M)
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
2:count統計某列的unique值
count(test,Species)
dplyr雙表操作:處理關系數據
#新建2表,注意不要引入factor,
options(stringsAsFactors = F)
> test1 <- data.frame(x = c('b','e','f','x'), z = c("A","B","C",'D'),stringsAsFactors = F)
> test2 <- data.frame(x = c('a','b','c','d','e','f'), y = c(1,2,3,4,5,6),stringsAsFactors = F)
> test1
x z
1 b A
2 e B
3 f C
4 x D
> test2
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
開始造
1.內連inner_join,取交集
> inner_join(test1, test2, by = "x")
x z y
1 b A 2
2 e B 5
3 f C 6
2.左連left_join
> left_join(test1, test2, by = 'x')
x z y
1 b A 2
2 e B 5
3 f C 6
4 x D NA
> left_join(test2, test1, by = 'x')
x y z
1 a 1 <NA>
2 b 2 A
3 c 3 <NA>
4 d 4 <NA>
5 e 5 B
6 f 6 C
3.全連full_join
> full_join( test1, test2, by = 'x')
x z y
1 b A 2
2 e B 5
3 f C 6
4 x D NA
5 a <NA> 1
6 c <NA> 3
7 d <NA> 4
4.半連接semi_join:返回能夠與y表匹配的x表所有記錄
> semi_join(x = test1, y = test2, by = 'x')
x z
1 b A
2 e B
3 f C
5.反連接anti_join:返回無法與y表匹配的x表的所記錄
> anti_join(x = test2, y = test1, by = 'x')
x y
1 a 1
2 c 3
3 d 4
6.簡單合并:bind_rows()按行合并, bind_cols()按列合并
#相當于base包里的cbind()函數和rbind()函數;注意,bind_rows()函數需要兩個表格列數相同,而bind_cols()函數則需要兩個數據框有相同的行數
> test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
> test1
x y
1 1 10
2 2 20
3 3 30
4 4 40
> test2 <- data.frame(x = c(5,6), y = c(50,60))
> test2
x y
1 5 50
2 6 60
> test3 <- data.frame(z = c(100,200,300,400))
> test3
z
1 100
2 200
3 300
4 400
> bind_rows(test1, test2)
x y
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
> bind_cols(test1, test3)
x y z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400