学习R包:
-
以dplyr包为例,先配置镜像,再下载安装包,最后加载包
1.先配置镜像:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
2.安装包:
install.packages("dplyr")
或者
BiocManager::install(“dplyr”)
运算结果如下:
> install.packages("dplyr")
also installing the dependencies ‘utf8’, ‘digest’, ‘cli’, ‘crayon’, ‘fansi’, ‘lifecycle’, ‘pillar’, ‘vctrs’, ‘purrr’, ‘ellipsis’, ‘assertthat’, ‘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")
运算结果如下:
> 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
-
示例数据直接使用内置数据集iris的简化版:
test <- iris[c(1:2,51:52,101:102),]
dplyr五个基础函数
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列 或 某几列 对整个表格进行排序
> arrange(test, Sepal.Length) #默认从小到大
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():汇总
对数据进行汇总操作,结合group_by使用实用性强
> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # 计算Sepal.Length的平均值和标准差
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的平均值和标准差
# 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两个实用技能
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统计某列的unique值
> count(test,Species)
# A tibble: 3 x 2
Species n
<fct> <int>
1 setosa 2
2 versicolor 2
3 virginica 2
dplyr处理关系数据
即将2个表进行连接,注意:不要引入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.內连 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.反连接:返回无法与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.简单合并
相当于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) #bind_rows()函数需要两个表格 列数 相同
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()函数则需要两个数据框有相同的 行数
x y z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400