是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了与其它数据库的接口;tidyr包的作者是Hadley Wickham, 该包用于“tidy”你的数据,这个包常跟dplyr结合使用。

本文将介绍dplyr包的下述五个函数用法:

筛选: filter()
排列: arrange()
选择: select()
变形: mutate()
汇总: summarise()
分组: group_by()
以及tidyr包的下述四个函数用法:

gather—宽数据转为长数据;
spread—长数据转为宽数据;
unit—多列合并为一列;
separate—将一列分离为多列;
 dplyr、tidyr包安装及载入

install.packages("dplyr")
install.packages("tidyr")

library(dplyr)
library(tidyr)
使用datasets包中的mtcars数据集做演示,首先将过长的数据整理成友好的tbl_df数据:

mtcars_df = tbl_df(mtcars)
dplyr包基本操作

 1.1 筛选: filter()

按给定的逻辑判断筛选出符合要求的子数据集

filter(mtcars_df,mpg==21,hp==110)

#  A tibble: 2 x 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1    21     6   160   110   3.9 2.620 16.46     0     1     4     4
2    21     6   160   110   3.9 2.875 17.02     0     1     4     4
 1.2 排列: arrange()

按给定的列名依次对行进行排序:

arrange(mtcars_df, disp) #可对列名加 desc(disp) 进行倒序

# A tibble: 32 x 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1   33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
2   30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2
3   32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
4   27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
5   30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
6   22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
7   21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1
8   26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
9   21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
10  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
# ... with 22 more rows
 1.3 选择: select()

用列名作参数来选择子数据集:

select(mtcars_df, disp:wt)

# A tibble: 32 x 4
    disp    hp  drat    wt
*  <dbl> <dbl> <dbl> <dbl>
1  160.0   110  3.90 2.620
2  160.0   110  3.90 2.875
3  108.0    93  3.85 2.320
4  258.0   110  3.08 3.215
5  360.0   175  3.15 3.440
6  225.0   105  2.76 3.460
7  360.0   245  3.21 3.570
8  146.7    62  3.69 3.190
9  140.8    95  3.92 3.150
10 167.6   123  3.92 3.440
# ... with 22 more rows
 1.4 变形: mutate()

对已有列进行数据运算并添加为新列:

mutate(mtcars_df,
  NO = 1:dim(mtcars_df)[1])

# A tibble: 32 x 12
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    NO
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1   21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4     1
2   21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4     2
3   22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     3
4   21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1     4
5   18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2     5
6   18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1     6
7   14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4     7
8   24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     8
9   22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     9
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    10
# ... with 22 more rows
 1.5 汇总: summarise()

对数据框调用其它函数进行汇总操作, 返回一维的结果:

summarise(mtcars_df, 
    mdisp = mean(disp, na.rm = TRUE))
# A tibble: 1 x 1
     mdisp
     <dbl>
1 230.7219
 1.6 分组: group_by()

当对数据集通过group_by()添加了分组信息后,mutate(),arrange() 和 summarise() 函数会自动对这些 tbl 类数据执行分组操作。

cars <- group_by(mtcars_df, cyl)
countcars <- summarise(cars, count = n()) # count = n()用来计算次数

# A tibble: 3 x 2
    cyl count
  <dbl> <int>
1     4    11
2     6     7
3     8    14
tidyr包基本操作

 2.1 宽转长:gather()

使用gather()函数实现宽表转长表,语法如下:

gather(data, key, value, …, na.rm = FALSE, convert = FALSE)
data:需要被转换的宽形表
key:将原数据框中的所有列赋给一个新变量key
value:将原数据框中的所有值赋给一个新变量value
…:可以指定哪些列聚到同一列中
na.rm:是否删除缺失值

widedata <- data.frame(person=c('Alex','Bob','Cathy'),grade=c(2,3,4),score=c(78,89,88))
widedata
  person grade score
1   Alex     2    78
2    Bob     3    89
3  Cathy     4    88
longdata <- gather(widedata, variable, value,-person)
longdata
  person variable value
1   Alex    grade     2
2    Bob    grade     3
3  Cathy    grade     4
4   Alex    score    78
5    Bob    score    89
6  Cathy    score    88
 2.2 长转宽:spread()

有时,为了满足建模或绘图的要求,往往需要将长形表转换为宽形表,或将宽形表变为长形表。如何实现这两种数据表类型的转换。使用spread()函数实现长表转宽表,语法如下:

spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)
data:为需要转换的长形表
key:需要将变量值拓展为字段的变量
value:需要分散的值
fill:对于缺失值,可将fill的值赋值给被转型后的缺失值

mtcarsSpread <- mtcarsNew %>% spread(attribute, value)
head(mtcarsSpread)
                 car am carb cyl disp drat gear  hp  mpg  qsec vs    wt
1        AMC Javelin  0    2   8  304 3.15    3 150 15.2 17.30  0 3.435
2 Cadillac Fleetwood  0    4   8  472 2.93    3 205 10.4 17.98  0 5.250
3         Camaro Z28  0    4   8  350 3.73    3 245 13.3 15.41  0 3.840
4  Chrysler Imperial  0    4   8  440 3.23    3 230 14.7 17.42  0 5.345
5         Datsun 710  1    1   4  108 3.85    4  93 22.8 18.61  1 2.320
6   Dodge Challenger  0    2   8  318 2.76    3 150 15.5 16.87  0 3.520
 2.3 合并:unit()

unite的调用格式如下:

unite(data, col, …, sep = “_”, remove = TRUE)
data:为数据框
col:被组合的新列名称
…:指定哪些列需要被组合
sep:组合列之间的连接符,默认为下划线
remove:是否删除被组合的列

wideunite<-unite(widedata, information, person, grade, score, sep= "-")
wideunite
  information
1   Alex-2-78
2    Bob-3-89
3  Cathy-4-88
 2.4 拆分:separate()

separate()函数可将一列拆分为多列,一般可用于日志数据或日期时间型数据的拆分,语法如下:

separate(data, col, into, sep = “[^[:alnum:]]+”, remove = TRUE,
convert = FALSE, extra = “warn”, fill = “warn”, …)
data:为数据框
col:需要被拆分的列
into:新建的列名,为字符串向量
sep:被拆分列的分隔符
remove:是否删除被分割的列

widesep <- separate(wideunite, information,c("person","grade","score"), sep = "-")
widesep
  person grade score
1   Alex     2    78
2    Bob     3    89
3  Cathy     4    88
可见separate()函数和unite()函数的功能相反。

dplyr包的更多相关文章

  1. dplyr包--数据操作与清洗

    1.简介 在我们数据分析的实际应用中,我们可能会花费大量的时间在数据清洗上,而如果使用 R 里面自带的一些函数(base 包的 transform 等),可能会觉得力不从心,或者不是很人性化.好在我们 ...

  2. R语言dplyr包初探

    昨天学了一下R语言dplyr包,处理数据框还是很好用的.记录一下免得我忘记了... 先写一篇入门的,以后有空再写一篇详细的用法. #dplyr learning library(dplyr) #fil ...

  3. 数据处理包plyr和dplyr包的整理

    以下内容主要参照 Introducing dplyr 和 dplyr 包自带的简介 (Introduction to dplyr), 复制了原文对应代码, 并夹杂了个人理解和观点 (多附于括号内). ...

  4. R Tidyverse dplyr包学习笔记2

    Tidyverse 学习笔记 1.gapminder 我理解的gapminder应该是一个内置的数据集 加载之后使用 > # Load the gapminder package > li ...

  5. R语言数据处理包dplyr、tidyr笔记

    dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了与其它数据库的接口:tidyr包的作者是Hadley ...

  6. 数据处理包dplyr的函数

    dplyr专注处理dataframe对象, 并提供更稳健的与其它数据库对象间的接口. 一.5个关键的数据处理函数: select()   返回列的子集filter()     返回行的子集arrang ...

  7. R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...

  8. R(6): 数据处理包dplyr

    dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了与其它数据库的接口,本节学习dplyr包函数基本用法 ...

  9. R语言中的数据处理包dplyr、tidyr笔记

    R语言中的数据处理包dplyr.tidyr笔记   dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了 ...

随机推荐

  1. Objective的头文件@interface属性

    源码:http://files.cnblogs.com/ios8/TestPropertyDemo.zip 1 前言 最近有个疑惑 @interface中的属性和@property声明的属性有什么区别 ...

  2. 关于iPhone音频的那些事

    音频文件(Audio File) 1.有两个概念(1).文件格式(File Format or Audio Containers)——描述文件本身的格式,里面的音频数据可以用不同的编码格式.例如:ca ...

  3. 常用sql自定义函数

    --把数字转为千分位格式的字符串显示,调用如 select dbo.f_splitstr(11111111111111) CREATE FUNCTION [dbo].[f_splitstr]( @st ...

  4. 解决Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK的问题

    Android Studio 运行AVD的时候出现: Installation failed with message Failed to finalize session : INSTALL_FAI ...

  5. adb devices连接提示 Android offline或unauthorized的解决办法

    我有一个华为X1手机,版本是Android 4.2.2(我的手机是Android 4.0版本), 但之前用adb连接(包括usb/tcp)时总莫名其妙出现offline或unauthorized的问题 ...

  6. [Windows Azure] Guidelines for Connecting to Windows Azure SQL Database

    Guidelines for Connecting to Windows Azure SQL Database 6 out of 12 rated this helpful - Rate this t ...

  7. linux系统卡解决方案

    1.查看内存使用率 free -g 运行结果: 2.查看磁盘使用率 df -h 运行结果: 3.查看磁盘IO iostat -x 1 运行结果: 60表示60秒钟刷新一次 4.查看CPU使用情况 to ...

  8. System.in的用法

    方法1 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));Scanner scanner=new Sca ...

  9. MySQL字段数据全部查出【只保留中文、英文、数字、空格的词表】

    select * from xxx_xxx_bak where slot_type_id in ('xxx', 'xxx') ; by @大超超 记录备查

  10. ZooKeeper管理分布式环境中的数据

    Reference: http://www.cnblogs.com/wuxl360/p/5817549.html 本节本来是要介绍ZooKeeper的实现原理,但是ZooKeeper的原理比较复杂,它 ...