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
mutate(test, new = Sepal.Length * Sepal.Width)
意为新增一列,test数据框中 Sepal.Length与Sepal.Width相乘的结果
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
select(test,1)
意为筛选出,test数据框中的第一列
> 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数据框中的第一和第五列
> 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的列
> 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的列
> 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向量中包含名字的列
> 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
filter(test, Species == "setosa")
意为筛选出test数据框中,Species列中setosa所在的行
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的行
> 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的行
> 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()#默认从小到大排序
所以arrange(test, Sepal.Length),意为在test数据框中,将Sepal.Length一列从小到大排序
> 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一列从大到小排序
> 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
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
#计算Sepal.Length的平均值和标准差
> 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的平均值和标准差
> 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
%>%
上述summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))可使用%>% 拆解为
group_by(test,Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
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
> count(test,Species)
Species n
1 setosa 2
2 versicolor 2
3 virginica 2
将两个表进行连接
> 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
> inner_join(test1, test2, by = "x")
x z y
1 b A 2
2 e B 5
3 f C 6
left_join(test1, test2, by = 'x')
根据test1数据框中的x列,取test2中test1x列对应的y值
> 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值
> 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
full_join( test1, test2, by = 'x')
将test1级test2数据框按x列取并集,并补齐相应的y列z列
> 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
> semi_join(x = test1, y = test2, by = 'x')
x z
1 b A
2 e B
3 f C
> anti_join(x = test2, y = test1, by = 'x')
x y
1 a 1
2 c 3
3 d 4
> 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()函数则需要两个数据框有相同的行数
> 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
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。