tapply()返回数据帧 [英] `tapply()` to return data frame

查看:78
本文介绍了tapply()返回数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,该数据集具有一个日期时间(POSIXct),一个节点"(因子)和一个"c"(数字)列,例如:

I have a dataset with a datetime (POSIXct), a "node" (factor) and and a "c" (numeric) columns, for example:

                 date node           c
1 2011-08-14 10:30:00    2 0.051236000
2 2011-08-14 10:30:00    2 0.081230000
3 2011-08-14 10:31:00    1 0.000000000
4 2011-08-14 10:31:00    4 0.001356337
5 2011-08-14 10:31:00    3 0.001356337
6 2011-08-14 10:32:00    2 0.000000000

我需要对所有日期"和节点"对取"c"列的平均值,所以我这样做了:

I need to take the mean of column "c" for all pairs of "date" and "node", so I did this:

tapply(data$c, list(data$node, data$date), mean)

我得到的结果是我想要的,但是结构很奇怪:

The result I obtain is what I want, but in a strange structure:

num [1:5, 1:8923] 0 0 0.00092 0.00146 NA ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5] "1" "2" "3" "4" ...
  ..$ : chr [1:8923] "2011-08-14 10:30:00" "2011-08-14 10:31:00" "2011-08-14 10:32:00" "2011-08-14 10:33:00" ...

示例输出为:

  2011-08-17 23:56:00 2011-08-17 23:57:00 2011-08-17 23:58:00
1        4.759077e-05        4.759077e-05        4.759077e-05
2        0.000000e+00        3.875248e-05        1.595690e-04
3        1.134391e-03        1.134391e-03        1.109730e-03
4        4.882813e-04        6.914658e-04        4.955846e-04
5        0.000000e+00        0.000000e+00        0.000000e+00

我想要的是类似于原始结构的东西,带有日期时间,节点因子和"c"值.我不知道如何实现这一目标.任何帮助将不胜感激.

What I was going for was something like the original structure, with a datetime, the node factor and the "c" value. I cannot figure out how to achieve this. Any help would be appreciated.

非常感谢.

推荐答案

您可以尝试...

aggregate( c ~ node + date, data = data, FUN = mean )

这篇关于tapply()返回数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆