分享

“了不起的R语言”第六课:tidyr包的应用

 无名小卒917 2017-03-11

“了不起的R语言”第六课:tidyr包的应用

前面对dplyr, reshape,reshape2包进行了简单的介绍. 下面介绍tidyr. tidyr包是reshape2包的进化版本, 其作者仍是Hadley Wickham, 往往与dplyr包结合使用, 目前渐有取代reshape2包之势, 是值得关注的一个R.

这里主要介绍tidyr包中下述四个常用函数的用法:

(1) gather(): 将宽数据转为长数据, 类似于reshape2包中的melt()函数;

(2) spread(): 将长数据转为宽数据, 类似于reshape2包中的cast()函数;

(3) unit(): 将多列合并为一列;

(4) separate(): 将一列分离为多列.

首先进行安装:

install.packages("tidyr").

安装成功后, 这里使用自编数据widedata2.csv进行数据分析.

自行导入widedata.csv数据:

widedata<-read.csv("widedata.csv")

widedata

结果如下:

 person  grade score 

1  Alex    2    78  

2  Bob    3    89  

3  Cathy   4    80  

 

gather()函数

gather()的调用格式为:

gather(data, key, value, … , na.rm = FALSE, convert = FALSE)

data是要调用的数据或数据框, key是宽数据变为长数据时存放需要编码的变量的变量名称, value是需要数据转换的变量的数值. 如执行下面的命令:

library(dplyr)

library(tidyr)

longdata <- gather(widedata, variable, value)

结果如下:

   variable   value

  1  person  Alex

  2  person  Bob

  3  person  Cathy

  4  grade   2

  5  grade   3

  6  grade   4

  7  score   78

  8  score   89

  9  score   80

gather()提供了只把制定变量从宽数据变成长数据的功能. 而执行下面的命令, 可以保留person不变成长数据.

gather(widedata, variable, value, -person)

结果如下:

 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      80 

可以看到, 除了person以外, 其余变量变成两列变量, 分别命名为variablevalue. 这正是gather()函数比reshape2包中melt()函数的优势所在: 它可以只gather若干列而其他列保持不变.

gather()还可迅速地整合两个变量之间的若干列, 而保持其他列不变. 为说明这一点, 这里先对widedata增加一列: age. 具体如下:

age <- c(20, 21, 22)

wide <- data.frame(widedata, age)

wide

结果如下:

   person  grade score  age 

1  Alex      2    78    20 

2  Bob       3    89    21 

3  Cathy     4    80    22 

再输入如下命令:

long <- gather(widedata1, variable, value, grade:age)

long

结果如下:

   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    80 

7  Alex    age      20 

8  Bob     age      21 

9  Cathy   age      22 

 

spread()函数

spread()的调用格式为:

spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)

spread()函数的功能和reshape2包中的cast()函数相似. 结合上面生成的widedatanew数据框, 可以利用下面的命令将长数据变成宽数据.

wide <- spread(long, variable, value)

wide

结果如下:

  person   age grade  score 

1  Alex    20  2   78 

2  Bob     21  3   89 

3  Cathy   22  4   80 

这实际将原来gather后的结果还原为gather前的情形, 但各列的相互位置稍有调整.

 

unite()函数

unite()的调用格式如下:

unite(data, col, … , sep = " ")

其中, data表示数据框, col表示合并后的列名称, … 表示需要合并的若干变量,sep = " "用于指定分隔符.

比如要把widedata中的person,grade, score三个变量合成一个变量information, 并变成"person-grade-score"的格式, 可通过下面的命令实现:

wideunite<-unite(widedata, information, person, grade, score, sep= "-")

wideunite

结果如下:

       information

  1    Alex-2-78

  2    Bob-3-89

  3    Cathy-4-80

 

separate()函数

separate()的调用格式为:

separate(data, col, into, sep = " ")

其中, data表示数据框, col代表要拆分的变量,into代表要拆分为的(多个), 通常用c()的形式进行命名, sep = " " 用于指定分隔符.

可用separate函数将上面的wideunite数据框还原. 执行下面的命令:

widesep <- separate(wideunite, information,c("person","grade","score"), sep = "-")

widesep

结果如下:

     person   grade score

1    Alex       2     78

2    Bob        3     89

3    Cathy      4     80

可见separate()函数和unite()函数的功能是相反的.

理解上述例子后, 读者也可用系统自带的mtcars等数据框深入练**tidyr数据包中的相关函数.

 

编辑:张光耀

 

本文档为南开大学周恩来政府管理学院研究生R语言课程(教师:吕小康)的课堂讲稿。

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多