查看dplyr包中有哪些函数
library(dplyr)
ls('package:dplyr')
#目前有290个包
1:筛选函数
- 1.1 filter函数⚠️ 针对行进行操作,提取一个或多个分组变量中的某个观测
library(dplyr)
library(reshape2) #使用的演示数据集来自这个包
#选择tips数据框中非吸烟和周日的行进行筛选
sub <- filter(tips,tips$smoker=='No',tips$day=='Sun')
head(sub)
# total_bill tip sex smoker day time size
# 1 16.99 1.01 Female No Sun Dinner 2
# 2 10.34 1.66 Male No Sun Dinner 3
# 3 21.01 3.50 Male No Sun Dinner 3
# 4 23.68 3.31 Male No Sun Dinner 2
# 5 24.59 3.61 Female No Sun Dinner 4
# 6 25.29 4.71 Male No Sun Dinner 4
⚠️filter和%in%结合使用,可以用于选取一个列中的多个分类变量
sub1 <- filter(tips,day %in% c('Sat','Sun'))
filter()只能筛选出条件为TRUE的行,它会排除那些条件为FALSE和NA的行。
注意:filter函数约等于subsets,但subsets既可以对行进行操作,也可以对列进行操作。
- 1.2. slice函数 针对行进行操作,可以提取指定行数
sub2 <- slice(tips,1:5) #tips是要操作的数据框,1:5是提取的行
sub2
# total_bill tip sex smoker day time size
# 1 16.99 1.01 Female No Sun Dinner 2
# 2 10.34 1.66 Male No Sun Dinner 3
# 3 21.01 3.50 Male No Sun Dinner 3
# 4 23.68 3.31 Male No Sun Dinner 2
# 5 24.59 3.61 Female No Sun Dinner 4
- 1.3. select函数⚠️ 针对列进行操作
sub3 <- select(tips,tip,sex,smoker) #提取tips中的tip, sex, smoker这三列
head(sub3)
# tip sex smoker
# 1 1.01 Female No
# 2 1.66 Male No
# 3 3.50 Male No
# 4 3.31 Male No
# 5 3.61 Female No
# 6 4.71 Male No
sub4 <- select(tips,2:5) #提取tips中的2-5列
head(sub4)
# tip sex smoker day
# 1 1.01 Female No Sun
# 2 1.66 Male No Sun
# 3 3.50 Male No Sun
# 4 3.31 Male No Sun
# 5 3.61 Female No Sun
# 6 4.71 Male No Sun
sub5 <- select(tips,tip:time) #提取tips中从tip到time所有的列
head(sub5)
# tip sex smoker day time
# 1 1.01 Female No Sun Dinner
# 2 1.66 Male No Sun Dinner
# 3 3.50 Male No Sun Dinner
# 4 3.31 Male No Sun Dinner
# 5 3.61 Female No Sun Dinner
# 6 4.71 Male No Sun Dinner
2. arrange函数(排序函数)⚠️
new_tips <- arrange(tips,total_bill,tip) #如果total_bill是一样的,就按tip排序
head(new_tips)
# total_bill tip sex smoker day time size
# 68 3.07 1.00 Female Yes Sat Dinner 1
# 93 5.75 1.00 Female Yes Fri Dinner 2
# 112 7.25 1.00 Female No Sat Dinner 1
# 173 7.25 5.15 Male Yes Sun Dinner 2
# 150 7.51 2.00 Male No Thur Lunch 2
# 196 7.56 1.44 Male No Thur Lunch 2
根据total_bill和tips对数据框进行排序(默认升序)
#降序
new_tips <- arrange(tips,desc(total_bill),tip)
head(new_tips)
# total_bill tip sex smoker day time size
# 171 50.81 10.00 Male Yes Sat Dinner 3
# 213 48.33 9.00 Male No Sat Dinner 4
# 60 48.27 6.73 Male No Sat Dinner 4
# 157 48.17 5.00 Male No Sun Dinner 6
# 183 45.35 3.50 Male Yes Sun Dinner 3
# 103 44.30 2.50 Female Yes Sat Dinner 3
缺失值总是排在最后
3. rename函数(对列进行重新命名)
new_tips <- rename(tips,bill=total_bill)
head(new_tips)
# bill tip sex smoker day time size
# 1 16.99 1.01 Female No Sun Dinner 2
# 2 10.34 1.66 Male No Sun Dinner 3
# 3 21.01 3.50 Male No Sun Dinner 3
# 4 23.68 3.31 Male No Sun Dinner 2
# 5 24.59 3.61 Female No Sun Dinner 4
# 6 25.29 4.71 Male No Sun Dinner 4
4. distinct函数(与levels函数有异曲同工之妙)
levels(tips$sex)
# [1] "Female" "Male"
distinct(tips,sex)
# sex
# 1 Female
# 2 Male
distinct(tips,day)
# day
# 1 Sun
# 20 Sat
# 78 Thur
# 91 Fri
5. mutate函数 & transform函数(生成新的变量)⚠️
head(mutate(tips,rate=tip/total_bill))
# total_bill tip sex smoker day time size rate
# 1 16.99 1.01 Female No Sun Dinner 2 0.05944673
# 2 10.34 1.66 Male No Sun Dinner 3 0.16054159
# 3 21.01 3.50 Male No Sun Dinner 3 0.16658734
# 4 23.68 3.31 Male No Sun Dinner 2 0.13978041
# 5 24.59 3.61 Female No Sun Dinner 4 0.14680765
# 6 25.29 4.71 Male No Sun Dinner 4 0.18623962
新生成了rate变量
head(mutate(tips,rate=tip/total_bill,new_rat=rate*100))
# total_bill tip sex smoker day time size rate new_rat
# 1 16.99 1.01 Female No Sun Dinner 2 0.05944673 5.944673
# 2 10.34 1.66 Male No Sun Dinner 3 0.16054159 16.054159
# 3 21.01 3.50 Male No Sun Dinner 3 0.16658734 16.658734
# 4 23.68 3.31 Male No Sun Dinner 2 0.13978041 13.978041
# 5 24.59 3.61 Female No Sun Dinner 4 0.14680765 14.680765
# 6 25.29 4.71 Male No Sun Dinner 4 0.18623962 18.623962
rate和new_rate可以同步生成
transform函数与mutate函数的不同之处在于:mutate函数 可以同时生成有递进关系的多个变量,而 transform函数只能一个一个生成。
head(transform(tips,rate=tip/total_bill,new_rat=rate*100))
# Error in eval(substitute(list(...)), `_data`, parent.frame()) :
# object 'rate' not found
transform函数必须先生成rate再生成new_rate,mutate函数可以同时生成rate和new_rate。
如果只想保留新变量,可以使用transmute函数()
。
6. sample_n函数 & sample_frac函数(在数据框中随机抽取一些行)
sample_n(iris,size=10) #从iris里随机抽取了10行
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 6.4 2.8 5.6 2.1 virginica
# 2 4.4 3.2 1.3 0.2 setosa
# 3 4.3 3.0 1.1 0.1 setosa
# 4 7.0 3.2 4.7 1.4 versicolor
# 5 5.4 3.0 4.5 1.5 versicolor
# 6 5.4 3.4 1.7 0.2 setosa
# 7 7.6 3.0 6.6 2.1 virginica
# 8 6.1 2.8 4.7 1.2 versicolor
# 9 4.6 3.4 1.4 0.3 setosa
# 10 6.3 2.5 4.9 1.5 versicolor
sample_frac(iris,0.1) #从iris里随机抽取了10%(0.1)行的数据(iris数据框一共150行,返回了15行)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 6.0 2.9 4.5 1.5 versicolor
# 2 5.5 3.5 1.3 0.2 setosa
# 3 6.5 3.0 5.8 2.2 virginica
# 4 7.2 3.6 6.1 2.5 virginica
# 5 5.5 4.2 1.4 0.2 setosa
# 6 7.6 3.0 6.6 2.1 virginica
# 7 7.2 3.2 6.0 1.8 virginica
# 8 5.6 3.0 4.1 1.3 versicolor
# 9 5.2 4.1 1.5 0.1 setosa
# 10 6.0 2.7 5.1 1.6 versicolor
# 11 5.6 2.5 3.9 1.1 versicolor
# 12 6.1 2.8 4.7 1.2 versicolor
# 13 4.5 2.3 1.3 0.3 setosa
# 14 6.5 3.2 5.1 2.0 virginica
# 15 5.1 3.8 1.5 0.3 setosa
7. group_by 分组函数⚠️(可以根据数据框中的分类变量进行分组,然后结合summarise函数进行汇总操作)
group=group_by(tips,smoker)
summarise(group,count=n(),mean_tips=mean(tip),sd_bill=sd(total_bill))
# A tibble: 2 x 4
# smoker count mean_tips sd_bill
# <fct> <int> <dbl> <dbl>
# 1 No 151 2.99 8.26
# 2 Yes 93 3.01 9.83
使用group_by函数,根据smoker对tips进行分组。之后采用summarize函数对分组数据进行统计。如上分别计算了smoker和non-smoker的个数、均值和标准差
8. 管道符 %>%⚠️
result <- tips %>% group_by(smoker,sex) %>% summarise(count = n(),mean_tips=mean(tip),sd_bill=sd(total_bill))
result
# A tibble: 4 x 5
# Groups: smoker [2]
# smoker sex count mean_tips sd_bill
# <fct> <fct> <int> <dbl> <dbl>
# 1 No Female 54 2.77 7.29
# 2 No Male 97 3.11 8.73
# 3 Yes Female 33 2.93 9.19
# 4 Yes Male 60 3.05 9.91
9. join函数家族(对数据框进行合并)
- 9.1 inner_join函数(⚠️和merge一样)
df_a <- data.frame(x=c('a','b','c','a','c','b','c'),y=1:7)
df_b <- data.frame(x=c('a','b','a'),z=10:12)
inner_join(df_a,df_b,by='x')
# x y z
# 1 a 1 10
# 2 a 1 12
# 3 b 2 11
# 4 a 4 10
# 5 a 4 12
# 6 b 6 11
根据共有的x来对数据框进行合并,由于第二个数据框中的x没有c,因而c被删掉了未被合并
- 9.2 semi_join函数
semi_join(df_a,df_b,by='x')
# x y
# 1 a 1
# 2 b 2
# 3 a 4
# 4 b 6
与inner_join类似,但只返回合并后的x和y
- 9.3 anti_join函数
anti_join(df_a,df_b,by='x')
# x y
# 1 c 3
# 2 c 5
# 3 c 7
与semi_join完全相反,只返回两个数据框中没有重复的值
- 9.4 left_join
left_join(df_a,df_b,by='x')
# x y z
# 1 a 1 10
# 2 a 1 12
# 3 b 2 11
# 4 c 3 NA
# 5 a 4 10
# 6 a 4 12
# 7 c 5 NA
# 8 b 6 11
# 9 c 7 NA
两个数据框合并时右边的数据框向左边的数据框合并,如果左边的数据框有右边数据框没有的观测,返回NA值。
- 9.5 right_join
right_join(df_a,df_b,by='x')
# x y z
# 1 a 1 10
# 2 a 1 12
# 3 b 2 11
# 4 a 4 10
# 5 a 4 12
# 6 b 6 11
两个数据框合并时左边的数据框向右边的数据框合并,如果左边的数据框有右边数据框没有的观测,则不予显示。
10. count函数(对list中针对某个分组变量的各个观测值的数量进行统计)⚠️
count(tips,smoker)
# smoker n
#1 No 151
#2 Yes 93
11. summarise函数
summarise函数对数据进行统计描述,可以将数据框折叠成一行,常与group_by函数搭配使用。group_by()与summarise()的组合构成了使用dplyr包时最常用的操作之一:分组摘要。
比base包中的summary()更加灵活
mtcars %>%
summarise(mean = mean(disp), n = n()) #查看disp这一列的均值,n = n()看有多少个观测
# mean n
# 1 230.7219 32
# 根据某个变量对某一列分组并统计
mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n())
# cyl mean n
# <dbl> <dbl> <int>
# 1 4 105. 11
# 2 6 183. 7
# 3 8 353. 14
# 同时进行多种统计运算
mtcars %>%
group_by(cyl) %>%
summarise(qs = quantile(disp, c(0.25, 0.75)), prob = c(0.25, 0.75))
# `summarise()` has grouped output by 'cyl'. You can override using the `.groups` argument.
# A tibble: 6 x 3
# Groups: cyl [3]
# cyl qs prob
# <dbl> <dbl> <dbl>
# 1 4 78.8 0.25
# 2 4 121. 0.75
# 3 6 160 0.25
# 4 6 196. 0.75
# 5 8 302. 0.25
# 6 8 390 0.75
#更多应用见?summarise
网友评论