前言:第六天的任務(wù)是學習R包。小伙伴說的好,在R中和在現(xiàn)實中一樣——“包”治百??!
鏡像設(shè)置
Q:為什么要設(shè)置鏡像呢?
A:因為在使用Rstudio的時候配置一個國內(nèi)鏡像可加速包的下載
設(shè)置方法:
1.初級方法——Rstudio的程序設(shè)置
這個設(shè)置的是CRAN的鏡像,如果要下載Bioconductor的包,這個鏡像是沒有辦法用的;另外即使設(shè)置了這里,Rstudio也不是每次都能真的從CRAN去下載包,可以通過options()$repos來檢驗,但速度很慢
2.升級方法——運行這兩行代碼
# options函數(shù)就是設(shè)置R運行過程中的一些選項設(shè)置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對應(yīng)中科大源
# 當然可以換成其他地區(qū)的鏡像
這個方法也不是萬能的,重新打開R時可用options()$BioC_mirror檢查Bioconductor是否還是國內(nèi)鏡像,如果它回到國外的官方鏡像,請右轉(zhuǎn)→傳送門:你還在每次配置Rstudio的下載鏡像嗎?
安裝包&加載包命令
install.packages(“包”) #安裝包,注意有引號?。?!
library(包) #加載包
require(包) #也是加載包 可二選一
dplyr五個基礎(chǔ)函數(shù)
示例數(shù)據(jù)直接使用內(nèi)置數(shù)據(jù)集iris的簡化版 test <- iris[c(1:2,51:52,101:102),]
電腦出來數(shù)據(jù)顯示:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66
- 新增列→
mutate(文件名, new = 列名) - 篩選列:按列號→
select(文件名,列號);按列名→select(文件名, 列名1, 列名2)orvars <- c("列名1", "列名2") select(文件名, one_of(vars)) - 篩選行→
filter(文件名, 行名 == 篩選關(guān)鍵詞)
如:
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
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
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
- 按某一列或某幾列對整個表格進行排序 →
arrange(文件名, 列名)(默認從小到大排) - 匯總(結(jié)合group_by使用實用性強) →
summarise(文件名, mean(), sd() )
如:
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計算Sepal.Length的平均值和標準差
## mean(Sepal.Length) sd(Sepal.Length)
## 1 5.916667 0.8084965
# 先按照Species分組,計算每組Sepal.Length的平均值和標準差
group_by(test, Species)
## # A tibble: 6 x 5 #tibble是個data frame
## # Groups: Species [3]
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## * <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3 1.4 0.2 setosa
## 3 7 3.2 4.7 1.4 versicolor
## 4 6.4 3.2 4.5 1.5 versicolor
## 5 6.3 3.3 6 2.5 virginica
## 6 5.8 2.7 5.1 1.9 virginica
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
## # A tibble: 3 x 3
## Species `mean(Sepal.Length)` `sd(Sepal.Length)`
##
## 1 setosa 5 0.141
## 2 versicolor 6.7 0.424
## 3 virginica 6.05 0.354
dplyr兩個實用技能
1.管道操作 %>% (cmd/ctr + shift + M) #注意輸入法設(shè)置
用管道符號可加載任意一個tidyverse包
2.count統(tǒng)計某列的unique值
count(文件名,列名)
dplyr處理關(guān)系數(shù)據(jù)
將2個表進行連接
options(stringsAsFactors = F)
test1 <- data.frame(x = c('b','e','f','x'),
z = c("A","B","C",'D'),
stringsAsFactors = F)
test1
## x z
## 1 b A
## 2 e B
## 3 f C
## 4 x D
test2 <- data.frame(x = c('a','b','c','d','e','f'),
y = c(1,2,3,4,5,6),
stringsAsFactors = F)
test2
## x y
## 1 a 1
## 2 b 2
## 3 c 3
## 4 d 4
## 5 e 5
## 6 f 6
1.內(nèi)連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
## 2 b 2 A
## 3 c 3
## 4 d 4
## 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
## 6 c
## 7 d
4.半連接&反連接
semi_join(x = test1, y = test2, by = 'x')
anti_join(x = test2, y = test1, by = 'x')
5.簡單合并
在相當于base包里的cbind()函數(shù)和rbind()函數(shù)
注意,bind_rows()函數(shù)需要兩個表格列數(shù)相同,而bind_cols()函數(shù)則需要兩個數(shù)據(jù)框有相同的行數(shù)
示例:
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