1 导入数据
> library(readxl)
> mydata1=read_excel("D:/Desktop/EconomicData.xlsx",sheet="Sheet2")
2 head()函数展示数据前几行
> head(mydata1)
# A tibble: 6 x 9
prov year pgdp eduyear pfdi open area
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 安徽 2011 2.56 8.25 0.0717 0.328 2
2 安徽 2012 2.87 8.52 0.0911 0.347 2
3 安徽 2013 3.19 8.52 0.110 0.400 2
4 安徽 2014 3.43 8.73 0.125 0.436 2
5 安徽 2015 3.58 8.80 0.138 0.431 2
6 安徽 2016 3.94 8.57 0.158 0.439 2
# ... with 2 more variables: educost <dbl>,
# college <dbl>
3 dplyr包的使用
> install.packages("dplyr")
> library(dplyr)
3.1 arrange()函数对数据排序
> arrange(mydata1, year, prov)
# A tibble: 240 x 9
prov year pgdp eduyear pfdi open area
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 安徽 2011 2.56 8.25 0.0717 0.328 2
2 北京 2011 8.05 11.6 0.226 4.14 1
3 福建 2011 4.72 8.83 0.108 2.34 1
4 甘肃 2011 1.96 8.15 0.00176 0.197 3
5 广东 2011 5.07 9.33 0.134 6.19 1
6 广西 2011 2.52 8.61 0.0141 0.449 3
7 贵州 2011 1.64 7.59 0.00959 0.0916 3
8 海南 2011 2.88 8.88 0.112 0.990 2
9 河北 2011 3.39 8.67 0.0418 0.751 1
10 河南 2011 2.87 8.70 0.0694 0.245 2
# ... with 230 more rows, and 2 more variables:
# educost <dbl>, college <dbl>
说明:这里没有将排序后的数据赋值给新的变量,仅将结果输出到屏幕。arrange()函数不会改变原始数据mydata1的排序!
3.2 select()函数选择变量
> select(mydata1, prov, year, pgdp)
# A tibble: 240 x 3
prov year pgdp
<chr> <dbl> <dbl>
1 安徽 2011 2.56
2 安徽 2012 2.87
3 安徽 2013 3.19
4 安徽 2014 3.43
5 安徽 2015 3.58
6 安徽 2016 3.94
7 安徽 2017 4.32
8 安徽 2018 5.38
9 北京 2011 8.05
10 北京 2012 8.64
# ... with 230 more rows
3.3 distinct()函数删除重复值
> distinct(mydata1,area,prov,.keep_all=TRUE)
# A tibble: 30 x 9
prov year pgdp eduyear pfdi open area educost college
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 安徽 2011 2.56 8.25 0.0717 0.328 2 817. 116
2 北京 2011 8.05 11.6 0.226 4.14 1 737. 87
3 福建 2011 4.72 8.83 0.108 2.34 1 634. 87
4 甘肃 2011 1.96 8.15 0.00176 0.197 3 361. 44
5 广东 2011 5.07 9.33 0.134 6.19 1 1885. 134
6 广西 2011 2.52 8.61 0.0141 0.449 3 594. 74
7 贵州 2011 1.64 7.59 0.00959 0.0916 3 451. 48
8 海南 2011 2.88 8.88 0.112 0.990 2 173. 17
9 河北 2011 3.39 8.67 0.0418 0.751 1 845. 119
10 河南 2011 2.87 8.70 0.0694 0.245 2 1182. 123
# ... with 20 more rows
3.4 filter()函数查找样本
> filter(mydata1, prov == "北京", year == 2016)
# A tibble: 1 x 9
prov year pgdp eduyear pfdi open area educost
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 北京 2016 11.8 12.3 0.398 3.74 1 1193.
# ... with 1 more variable: college <dbl>
> filter(mydata1, prov == "北京", year > 2016)
# A tibble: 2 x 9
prov year pgdp eduyear pfdi open area educost
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 北京 2017 12.9 12.5 0.757 3.78 1 1251.
2 北京 2018 15.4 12.6 0.532 3.91 1 1353.
# ... with 1 more variable: college <dbl>
3.5 mutate()函数生成新变量
> mutate(mydata1,x1=educost/college,x2=pgdp*10000)
# A tibble: 240 x 11
prov year pgdp eduyear pfdi open area educost college x1 x2
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 安徽 2011 2.56 8.25 0.0717 0.328 2 817. 116 7.04 25638.
2 安徽 2012 2.87 8.52 0.0911 0.347 2 992. 118 8.40 28744.
3 安徽 2013 3.19 8.52 0.110 0.400 2 1041. 117 8.9 31891.
4 安徽 2014 3.43 8.73 0.125 0.436 2 1046. 118 8.86 34274.
5 安徽 2015 3.58 8.80 0.138 0.431 2 1158. 119 9.73 35819.
6 安徽 2016 3.94 8.57 0.158 0.439 2 1236. 119 10.4 39393.
7 安徽 2017 4.32 8.68 0.172 0.550 2 1375. 119 11.6 43196.
8 安徽 2018 5.38 8.94 0.178 0.622 2 1501. 119 12.6 53784.
9 北京 2011 8.05 11.6 0.226 4.14 1 737. 87 8.48 80511.
10 北京 2012 8.64 11.8 0.245 3.93 1 872. 89 9.79 86403.
说明:生成了校均教育经费变量x1和单位由亿元转化为万元的pgdp变量x2
3.6 group_by()函数分组统计
> summarise(mydata1,mean1 = mean(college, na.rm = TRUE))
# A tibble: 1 x 1
mean1
<dbl>
1 84.8
> groupdata=group_by(mydata1, area)
> summarise(groupdata,mean2 = mean(college, na.rm = TRUE))
# A tibble: 3 x 2
area mean2
<dbl> <dbl>
1 1 108.
2 2 87.9
3 3 58.1
说明:直接采用summarise()函数统计的是所有地区全部年份的平均学校数;采用groupby()函数分组后,可以分别计算出area1、area2和area3这三个地区的平均学校数。