學(xué)習(xí)R包:
-
以dplyr包為例姐呐,先配置鏡像,再下載安裝包徒欣,最后加載包
1.先配置鏡像:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對應(yīng)中科大源
2.安裝包:
install.packages("dplyr")
或者
BiocManager::install(“dplyr”)
運(yùn)算結(jié)果如下:
> install.packages("dplyr")
also installing the dependencies ‘utf8’, ‘digest’, ‘cli’, ‘crayon’, ‘fansi’, ‘lifecycle’, ‘pillar’, ‘vctrs’, ‘purrr’, ‘ellipsis’, ‘a(chǎn)ssertthat’, ‘glue’, ‘magrittr’, ‘pkgconfig’, ‘R6’, ‘Rcpp’, ‘rlang’, ‘tibble’, ‘tidyselect’, ‘BH’, ‘plogr’
There is a binary version available but the source version is later:
binary source needs_compilation
glue 1.3.2 1.4.0 TRUE
Do you want to install from sources the package which needs compilation? (Yes/no/cancel) yes
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/utf8_1.1.4.tgz'
Content type 'application/octet-stream' length 196648 bytes (192 KB)
==================================================
downloaded 192 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/digest_0.6.25.tgz'
Content type 'application/octet-stream' length 246357 bytes (240 KB)
==================================================
downloaded 240 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/cli_2.0.2.tgz'
Content type 'application/octet-stream' length 395137 bytes (385 KB)
==================================================
downloaded 385 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/crayon_1.3.4.tgz'
Content type 'application/octet-stream' length 749917 bytes (732 KB)
==================================================
downloaded 732 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/fansi_0.4.1.tgz'
Content type 'application/octet-stream' length 210779 bytes (205 KB)
==================================================
downloaded 205 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/lifecycle_0.2.0.tgz'
Content type 'application/octet-stream' length 91621 bytes (89 KB)
==================================================
downloaded 89 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/pillar_1.4.3.tgz'
Content type 'application/octet-stream' length 178277 bytes (174 KB)
==================================================
downloaded 174 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/vctrs_0.2.4.tgz'
Content type 'application/octet-stream' length 1077741 bytes (1.0 MB)
==================================================
downloaded 1.0 MB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/purrr_0.3.3.tgz'
Content type 'application/octet-stream' length 412501 bytes (402 KB)
==================================================
downloaded 402 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/ellipsis_0.3.0.tgz'
Content type 'application/octet-stream' length 33047 bytes (32 KB)
==================================================
downloaded 32 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/assertthat_0.2.1.tgz'
Content type 'application/octet-stream' length 53625 bytes (52 KB)
==================================================
downloaded 52 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/magrittr_1.5.tgz'
Content type 'application/octet-stream' length 154842 bytes (151 KB)
==================================================
downloaded 151 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/pkgconfig_2.0.3.tgz'
Content type 'application/octet-stream' length 17573 bytes (17 KB)
==================================================
downloaded 17 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/R6_2.4.1.tgz'
Content type 'application/octet-stream' length 57479 bytes (56 KB)
==================================================
downloaded 56 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/Rcpp_1.0.4.tgz'
Content type 'application/octet-stream' length 3124401 bytes (3.0 MB)
==================================================
downloaded 3.0 MB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/rlang_0.4.5.tgz'
Content type 'application/octet-stream' length 1182651 bytes (1.1 MB)
==================================================
downloaded 1.1 MB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/tibble_3.0.0.tgz'
Content type 'application/octet-stream' length 384238 bytes (375 KB)
==================================================
downloaded 375 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/tidyselect_1.0.0.tgz'
Content type 'application/octet-stream' length 240162 bytes (234 KB)
==================================================
downloaded 234 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/BH_1.72.0-3.tgz'
Content type 'application/octet-stream' length 11253901 bytes (10.7 MB)
==================================================
downloaded 10.7 MB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/plogr_0.2.0.tgz'
Content type 'application/octet-stream' length 13178 bytes (12 KB)
==================================================
downloaded 12 KB
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/el-capitan/contrib/3.6/dplyr_0.8.5.tgz'
Content type 'application/octet-stream' length 6859111 bytes (6.5 MB)
==================================================
downloaded 6.5 MB
The downloaded binary packages are in
/var/folders/7c/mpkw17q950jfp2gj5w1hc3vr0000gn/T//Rtmpk6zLHd/downloaded_packages
installing the source package ‘glue’
試開URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/glue_1.4.0.tar.gz'
Content type 'application/x-gzip' length 98619 bytes (96 KB)
==================================================
downloaded 96 KB
* installing *source* package ‘glue’ ...
** 成功將‘glue’程序包解包并MD5和檢查
** using staged installation
** libs
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c glue.c -o glue.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c init.c -o init.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c trim.c -o trim.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o glue.so glue.o init.o trim.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.6/Resources/library/00LOCK-glue/00new/glue/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (glue)
The downloaded source packages are in
‘/private/var/folders/7c/mpkw17q950jfp2gj5w1hc3vr0000gn/T/Rtmpk6zLHd/downloaded_packages’
3.加載包
library("dplyr")
或者
require("dplyr")
運(yùn)算結(jié)果如下:
> library("dplyr")
載入程輯包:‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
-
示例數(shù)據(jù)直接使用內(nèi)置數(shù)據(jù)集iris的簡化版:
test <- iris[c(1:2,51:52,101:102),]
dplyr五個基礎(chǔ)函數(shù)
1.mutate(), 新增 列
> mutate(test, new = Sepal.Length * Sepal.Width)
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
-
select() ,按 列 篩選
(1)按列號篩選
> select(test,1) #選取第一列
Sepal.Length
1 5.1
2 4.9
51 7.0
52 6.4
101 6.3
102 5.8
> select(test,c(1,5)) #選取第一列和第五列
Sepal.Length Species
1 5.1 setosa
2 4.9 setosa
51 7.0 versicolor
52 6.4 versicolor
101 6.3 virginica
102 5.8 virginica
(2)按列名篩選
> select(test, Petal.Length, Petal.Width)
Petal.Length Petal.Width
1 1.4 0.2
2 1.4 0.2
51 4.7 1.4
52 4.5 1.5
101 6.0 2.5
102 5.1 1.9
> vars <- c("Petal.Length", "Petal.Width")
> select(test, one_of(vars))
Petal.Length Petal.Width
1 1.4 0.2
2 1.4 0.2
51 4.7 1.4
52 4.5 1.5
101 6.0 2.5
102 5.1 1.9
- 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
> 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(), 按 某1列 或 某幾列 對整個表格進(jìn)行排序
> arrange(test, Sepal.Length) #默認(rèn)從小到大
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 4.9 3.0 1.4 0.2 setosa
2 5.1 3.5 1.4 0.2 setosa
3 5.8 2.7 5.1 1.9 virginica
4 6.3 3.3 6.0 2.5 virginica
5 6.4 3.2 4.5 1.5 versicolor
6 7.0 3.2 4.7 1.4 versicolor
> arrange(test, desc(Sepal.Length)) #desc表示從大到小
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.0 3.2 4.7 1.4 versicolor
2 6.4 3.2 4.5 1.5 versicolor
3 6.3 3.3 6.0 2.5 virginica
4 5.8 2.7 5.1 1.9 virginica
5 5.1 3.5 1.4 0.2 setosa
6 4.9 3.0 1.4 0.2 setosa
- summarise():匯總
對數(shù)據(jù)進(jìn)行匯總操作,結(jié)合group_by使用實(shí)用性強(qiáng)
> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # 計算Sepal.Length的平均值和標(biāo)準(zhǔn)差
mean(Sepal.Length) sd(Sepal.Length)
1 5.916667 0.8084965
> group_by(test, Species) #按照Species分組
# A tibble: 6 x 5
# 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)) #先按照Species分為3組,然后計算每組Sepal.Length的平均值和標(biāo)準(zhǔn)差
# A tibble: 3 x 3
Species `mean(Sepal.Length)` `sd(Sepal.Length)`
<fct> <dbl> <dbl>
1 setosa 5 0.141
2 versicolor 6.7 0.424
3 virginica 6.05 0.354
dplyr兩個實(shí)用技能
1.管道操作 %>% (即用鍵盤 cmd/ctr + shift + M 可打出 %>% )
加載任意一個tidyverse包即可用管道符號
> test %>%
+ group_by(Species) %>%
+ summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 x 3
Species `mean(Sepal.Length)` `sd(Sepal.Length)`
<fct> <dbl> <dbl>
1 setosa 5 0.141
2 versicolor 6.7 0.424
3 virginica 6.05 0.354
2.count統(tǒng)計某列的unique值
> count(test,Species)
# A tibble: 3 x 2
Species n
<fct> <int>
1 setosa 2
2 versicolor 2
3 virginica 2
dplyr處理關(guān)系數(shù)據(jù)
即將2個表進(jìn)行連接,注意:不要引入factor
> 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相同的部分翁涤,連接兩表
x z y
1 b A 2
2 e B 5
3 f C 6
2.左連 left_join
> left_join(test1, test2, by = 'x') #可以看到以NA來表示不存在的元素
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.半連接:返回能夠與y表匹配的x表所有記錄 semi_join
> semi_join(x = test1, y = test2, by = 'x')
x z
1 b A
2 e B
3 f C
5.反連接:返回?zé)o法與y表匹配的x表的所記錄 anti_join
> anti_join(x = test2, y = test1, by = 'x') #注意,y來源于test1
x y
1 a 1
2 c 3
3 d 4
6.簡單合并
相當(dāng)于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) #bind_rows()函數(shù)需要兩個表格 列數(shù) 相同
x y
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
> bind_cols(test1, test3) #bind_cols()函數(shù)則需要兩個數(shù)據(jù)框有相同的 行數(shù)
x y z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400