安装和加载R包
设置镜像、安装、加载(以dplyr包为例)
代码语言:R复制options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages("dplyr")
also installing the dependencies ‘cli’, ‘lifecycle’, ‘pillar’, ‘rlang’, ‘tibble’, ‘tidyselect’, ‘vctrs’
There are binary versions available but the source
versions are later:
binary source needs_compilation
rlang 1.1.2 1.1.3 TRUE
tidyselect 1.2.0 1.2.1 TRUE
Do you want to install from sources the packages which need compilation? (Yes/no/cancel) Yes
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/cli_3.6.2.tgz'
Content type 'application/octet-stream' length 1369741 bytes (1.3 MB)
downloaded 1.3 MB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/lifecycle_1.0.4.tgz'
Content type 'application/octet-stream' length 121623 bytes (118 KB)
downloaded 118 KB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/pillar_1.9.0.tgz'
Content type 'application/octet-stream' length 643056 bytes (627 KB)
downloaded 627 KB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/tibble_3.2.1.tgz'
Content type 'application/octet-stream' length 676165 bytes (660 KB)
downloaded 660 KB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/vctrs_0.6.5.tgz'
Content type 'application/octet-stream' length 1852246 bytes (1.8 MB)
downloaded 1.8 MB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/macosx/contrib/4.2/dplyr_1.1.4.tgz'
Content type 'application/octet-stream' length 1570597 bytes (1.5 MB)
downloaded 1.5 MB
The downloaded binary packages are in
/var/folders/v_/r2n80_ls6yx_37pjtzps5yqh0000gn/T//RtmpsObKqI/downloaded_packages
installing the source packages ‘rlang’, ‘tidyselect’
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/rlang_1.1.3.tar.gz'
Content type 'application/octet-stream' length 763765 bytes (745 KB)
downloaded 745 KB
trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/contrib/tidyselect_1.2.1.tar.gz'
Content type 'application/octet-stream' length 103591 bytes (101 KB)
downloaded 101 KB
* installing *source* package ‘rlang’ ...
** package ‘rlang’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I./rlang/ -I/usr/local/include -fPIC -Wall -g -O2 -c capture.c -o capture.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I./rlang/ -I/usr/local/include -fPIC -Wall -g -O2 -c internal.c -o internal.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I./rlang/ -I/usr/local/include -fPIC -Wall -g -O2 -c rlang.c -o rlang.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I./rlang/ -I/usr/local/include -fPIC -Wall -g -O2 -c version.c -o version.o
clang -mmacosx-version-min=10.13 -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 rlang.so capture.o internal.o rlang.o version.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/4.2/Resources/library/00LOCK-rlang/00new/rlang/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** 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 (rlang)
* installing *source* package ‘tidyselect’ ...
** package ‘tidyselect’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (tidyselect)
The downloaded source packages are in
‘/private/var/folders/v_/r2n80_ls6yx_37pjtzps5yqh0000gn/T/RtmpsObKqI/downloaded_packages’
library(dplyr)
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
dplyr五个基础函数
1.mutate()新增列
mutate(test, new = Sepal.Length * Sepal.Width)
意为新增一列,test数据框中 Sepal.Length与Sepal.Width相乘的结果
代码语言:R复制mutate(test, new = Sepal.Length * Sepal.Width)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
51 7.0 3.2 4.7 1.4
52 6.4 3.2 4.5 1.5
101 6.3 3.3 6.0 2.5
102 5.8 2.7 5.1 1.9
Species new
1 setosa 17.85
2 setosa 14.70
51 versicolor 22.40
52 versicolor 20.48
101 virginica 20.79
102 virginica 15.66
2.select()按列筛选
按列号筛选
select(test,1)
意为筛选出,test数据框中的第一列
代码语言:R复制> select(test,1)
Sepal.Length
1 5.1
2 4.9
51 7.0
52 6.4
101 6.3
select(test,c(1,5))
意思为筛选出,test数据框中的第一和第五列
代码语言:R复制> 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
select(test,Sepal.Length)
意为筛选出,test数据框中列名为Sepal.Length的列
代码语言:R复制> select(test,Sepal.Length)
Sepal.Length
1 5.1
2 4.9
51 7.0
52 6.4
101 6.3
按列名筛选
select(test, Petal.Length, Petal.Width)
意为筛选出,test数据框中列名为Sepal.Length和Petal.Width的列
代码语言:R复制> 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
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
将"Petal.Length", "Petal.Width"赋值给vas
意为筛选出test数据框,vas向量中包含名字的列
代码语言:R复制> 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
3.filter()按列筛选
filter(test, Species == "setosa")
意为筛选出test数据框中,Species列中setosa所在的行
代码语言:R复制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 )
意为筛选出test数据框中,Species列中setosa且Sepal.Length列值大于5的行
代码语言:R复制> 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"))
意为筛选出test数据框中,Species列中setosa和versicolor的行
代码语言:R复制> 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
4.arrange(),按某1列或某几列对整个表格进行排序
arrange()#默认从小到大排序
所以arrange(test, Sepal.Length),意为在test数据框中,将Sepal.Length一列从小到大排序
代码语言:R复制> 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
#用desc从大到小
所以arrange(test, desc(Sepal.Length)),意为在test数据框中,将Sepal.Length一列从大到小排序
代码语言:R复制> arrange(test, desc(Sepal.Length))
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
5.summarise()汇总
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
#计算Sepal.Length的平均值和标准差
代码语言:R复制> summarise(test, mean(Sepal.Length), sd(Sepal.Length))
mean(Sepal.Length) sd(Sepal.Length)
1 5.916667 0.8084965
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
意为先按照Species分组,计算每组Sepal.Length的平均值和标准差
代码语言:R复制> summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 × 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 shift M)
%>%
上述summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))可使用%>% 拆解为
group_by(test,Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
代码语言:R复制 group_by(test,Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 × 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值
代码语言:R复制> count(test,Species)
Species n
1 setosa 2
2 versicolor 2
3 virginica 2
dplyr处理关系数据
将两个表进行连接
代码语言:R复制> test1 <- data.frame(x = c('b','e','f','x'),
z = c("A","B","C",'D'))
> test2 <- data.frame(x = c('a','b','c','d','e','f'),
y = c(1,2,3,4,5,6))
> 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,取交集
代码语言:R复制> inner_join(test1, test2, by = "x")
x z y
1 b A 2
2 e B 5
3 f C 6
左连left_join
left_join(test1, test2, by = 'x')
根据test1数据框中的x列,取test2中test1x列对应的y值
代码语言:R复制> 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')
根据test2数据框中的x列,取test1中test2x列对应z值
代码语言:R复制> 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')
将test1级test2数据框按x列取并集,并补齐相应的y列z列
代码语言:R复制> 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
代码语言:R复制> semi_join(x = test1, y = test2, by = 'x')
x z
1 b A
2 e B
3 f C
5.反连接:返回无法与y表匹配的x表的所记录anti_join
代码语言:R复制> anti_join(x = test2, y = test1, by = 'x')
x y
1 a 1
2 c 3
3 d 4
6.简单合并
代码语言:R复制> test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
> test2 <- data.frame(x = c(5,6), y = c(50,60))
> test3 <- data.frame(z = c(100,200,300,400))
> test1
x y
1 1 10
2 2 20
3 3 30
4 4 40
> test2
x y
1 5 50
2 6 60
> test3
z
1 100
2 200
3 300
4 400
bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数
代码语言:R复制> bind_rows(test1, test2)
x y
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
代码语言:R复制> bind_cols(test1, test3)
x y z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400