第五章:数据操作Ⅱ 第三节:数据结构变形与汇总

本文介绍了如何使用R语言的reshape2包中的melt()和cast()函数进行数据重塑,包括将长格式数据转换为宽格式,以及计算测量变量的汇总统计。通过实例展示了如何处理french_fries数据,展示数据组织和汇总的过程。
摘要由CSDN通过智能技术生成

Reshape2包中包含大量数据整形和分组计算汇总统计量的函数,变换后的数据可以使用variable与value列表现测量值,所以更方便计算数据统计值,提供变换功能的函数主要由melt()和cast()承担

我们先安装reshape2包

install.packages("reshape2")

library(reshape2)

我们利用reshape2包中的french_frie数据来实现melt()和cast()函数功能

> head(french_fries)
   time treatment subject rep potato buttery grassy rancid painty
61    1         1       3   1    2.9     0.0    0.0    0.0    5.5
25    1         1       3   2   14.0     0.0    0.0    1.1    0.0
62    1         1      10   1   11.0     6.4    0.0    0.0    0.0
26    1         1      10   2    9.9     5.9    2.9    2.2    0.0
63    1         1      15   1    1.2     0.1    0.0    1.1    5.1
27    1         1      15   2    8.8     3.0    3.6    1.5    2.3

该数据主要是统计量炸薯条所用的食用油对薯条味道的影响,具体数据解析如下

(一)melt()函数

Melt()函数使用识别标识符、测量变量、测量值这3列重新组织数据

 注:

  • 标识符变量(ID variables):这些变量在转换后的数据中将保持不变,用于唯一标识每个观察值。
  • 测量变量(Measure variables):这些变量包含原始数据中的值,将成为转换后数据集中的一个新变量列。
  • 测量值(Value):这是测量变量对应的具体数值,将成为转换后数据集中的另一个新变量列。

根据上述数据探查发现,我们可以认为在french_fries数据中,timetreatmentsubjectrep可以作数据识别,而剩余的作为测量变量和测量值,因此我们可以使用melt()来进行数据组织

(自变量和因变量)

> m<-melt(french_fries,id.vars = 1:4)
> m
    time treatment subject rep variable value
1      1         1       3   1   potato   2.9
2      1         1       3   2   potato  14.0
3      1         1      10   1   potato  11.0
4      1         1      10   2   potato   9.9
5      1         1      15   1   potato   1.2
6      1         1      15   2   potato   8.8
7      1         1      16   1   potato   9.0

我们可以看到melt()函数将原来多个列的测量值转换为两列(variablevalue)多行形式,因此我们就可以很方便的统计数据,例如,我们要统计每种食用油(treatment)测量变量的平均值

> ddply(m,.(variable),summarise,mean=mean(value,na.rm=TRUE))
  variable      mean
1   potato 6.9525180
2  buttery 1.8236994
3   grassy 0.6641727
4   rancid 3.8522302
5   painty 2.5217579

 这边有点问题理解不了,为什么说treatment但是筛选条件没包含treatment

在相应行不存在NA值时,Complete.case()函数返回True,否则返回False,因此,我们可以轻松使用Complete.case()来查找含有NA的行

> french_fries[!complete.cases(french_fries),]
    time treatment subject rep potato buttery grassy rancid painty
315    5         3      15   1     NA      NA     NA     NA     NA
455    7         2      79   1    7.3      NA    0.0    0.7      0
515    8         1      79   1   10.5      NA    0.0    0.5      0
520    8         2      16   1    4.5      NA    1.4    6.7      0
563    8         2      79   2    5.7       0    1.4    2.3     NA

这边就将所以包含NA的行找了出来。

使用melt函数处理时,如果想要去除NA行,我们直接将na.rm设置为True即可

> m<-melt(id=1:4,french_fries,na.rm=TRUE)
> m
    time treatment subject rep variable value
1      1         1       3   1   potato   2.9
2      1         1       3   2   potato  14.0
3      1         1      10   1   potato  11.0
4      1         1      10   2   potato   9.9
5      1         1      15   1   potato   1.2

(二)cast()函数

cast()函数主要用于将长格式(long format)的数据框转换为宽格式(wide format)的数据框,或者进行数据聚合操作,我们只讲dcast()函数,以数据框形式返回

例如:下列代码中,我们先用melt()函数处理french_fries数据,再使用dcast()函数将数据恢复原貌,然后再比两者是否一致

>m<-melt(id=1:4,french_fries)
> r<-dcast(m,time+treatment+subject+rep~...)
> identical(r,french_fries)
[1] TRUE

(三)数据汇总

我们可以使用cast()函数进行数据汇总,我们只需要将较少的识别符号放入dcast()的formula即可,例如,在前面我们使用melt()函数时用到的识别符有time、treatment、subject、rep。在dcast()中复原时,要将其设置为time+treatmen+subject+rep~形式,但如果将这些识别符之一从公式~左侧排除,进行dcast()处理时,多个行就会集中到1个单元格,即可完成数据汇总

例如:

> m<-melt(french_fries,id.vars=1:4)
> dcast(m,time~variable)
Aggregation function missing: defaulting to length
   time potato buttery grassy rancid painty
1     1     72      72     72     72     72
2     2     72      72     72     72     72
3     3     72      72     72     72     72
4     4     72      72     72     72     72
5     5     72      72     72     72     72
6     6     72      72     72     72     72
7     7     72      72     72     72     72
8     8     72      72     72     72     72
9     9     60      60     60     60     60
10   10     60      60     60     60     60

这边默认使用length函数统计了出现的频数

同时我们也可以利用此来进行求每一个time的平均值,例如

> dcast(m,time~variable,mean,na.rm=TRUE)
   time   potato  buttery    grassy   rancid   painty
1     1 8.562500 2.236111 0.9416667 2.358333 1.645833
2     2 8.059722 2.722222 1.1819444 2.845833 1.444444
3     3 7.797222 2.102778 0.7500000 3.715278 1.311111
4     4 7.713889 1.801389 0.7416667 3.602778 1.372222
5     5 7.328169 1.642254 0.6352113 3.529577 2.015493
6     6 6.670833 1.752778 0.6736111 4.075000 2.341667
7     7 6.168056 1.369014 0.4208333 3.886111 2.683333
8     8 5.431944 1.182857 0.3805556 4.272222 3.938028
9     9 5.673333 1.586667 0.2766667 4.670000 3.873333
10   10 5.703333 1.765000 0.5566667 6.068333 5.291667

在调用dcast()函数的时候,~右侧用于指出测量变量,这些测量变量将结果中成为新的列。因此将标识符放在~右侧,得到与每个识别符值对应的variable的汇总值,并以新列表示,下列示例中,针对每个time,计算每个time,计算(treatment,variable)有序偶的value平均值。

> dcast(m,time~treatment+variable,mean,na.rm=TRUE)
   time 1_potato 1_buttery  1_grassy 1_rancid  1_painty 2_potato 2_buttery  2_grassy
1     1 7.925000 1.7958333 0.9041667 2.758333 2.1500000 8.775000  2.491667 0.9958333
2     2 7.591667 2.5250000 1.0041667 3.900000 1.9750000 8.537500  3.125000 0.9500000
3     3 7.770833 2.2958333 0.8166667 4.650000 1.1166667 7.637500  2.079167 0.7250000
4     4 8.404167 1.9791667 1.0250000 2.079167 0.4666667 8.204167  1.608333 0.6416667
5     5 7.741667 1.3666667 0.7708333 4.279167 3.0083333 6.933333  1.858333 0.5833333

在上述结果中,列名前面的数字代表treatment值

> ddply(m,.(time,treatment,variable),function(row){return(mean(row$value,na.rm=TRUE))})
    time treatment variable        V1
1      1         1   potato 7.9250000
2      1         1  buttery 1.7958333
3      1         1   grassy 0.9041667
4      1         1   rancid 2.7583333
5      1         1   painty 2.1500000
6      1         2   potato 8.7750000

 

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值