【问题标题】:aggregate POSIXct vector using plyr使用 plyr 聚合 POSIXct 向量
【发布时间】:2014-06-25 18:55:18
【问题描述】:

我正在尝试编写一个适用于 POSIXct 时间向量的 ddply 汇总语句。对于每个 user.nm,我只想获取与其名称相关的最大和最小时间戳。数据如下:

    test.data=structure(list(user.nm = structure(c(1L, 1L, 2L, 3L, 4L, 4L), .Label = c("a", 
"b", "c", "d"), class = "factor"), ip.addr.txt = structure(c(1L, 
2L, 3L, 4L, 5L, 5L), .Label = c("a", "b", "c", "d", "e"), class = "factor"), 
    login.dt = structure(c(4L, 3L, 5L, 1L, 2L, 6L), .Label = c("11/20/2013", 
    "12/26/2013", "3/11/2013", "6/25/2013", "6/27/2013", "7/15/2013"
    ), class = "factor"), login.time = structure(c(3L, 4L, 6L, 
    1L, 2L, 5L), .Label = c("10:16:17", "11:07:27", "13:22:32", 
    "13:55:05", "9:23:33", "9:49:23"), class = "factor"), login.sessn.ts = structure(c(1372180920, 
    1363024500, 1372340940, 1384960560, 1388074020, 1373894580
    ), class = c("POSIXct", "POSIXt"), tzone = ""), month = structure(c(3L, 
    4L, 3L, 5L, 1L, 2L), .Label = c("Dec-2013", "Jul-2013", "Jun-2013", 
    "Mar-2013", "Nov-2013"), class = "factor"), quarter = c(2L, 
    1L, 2L, 4L, 4L, 3L), change.label = c(TRUE, TRUE, TRUE, TRUE, 
    TRUE, TRUE)), .Names = c("user.nm", "ip.addr.txt", "login.dt", 
"login.time", "login.sessn.ts", "month", "quarter", "change.label"
), row.names = c(NA, -6L), class = "data.frame")

plyr 语句如下所示:

user.changes=ddply(test.data, c("user.nm"), summarize, 
               change.count=sum(ip.label.txt),
               max.change.time=max(login.sessn.ts),
               min.change.time=min(login.sessn.ts))

我得到的错误是:

Error in attributes(out) <- attributes(col) : 
  'names' attribute [9] must be the same length as the vector [2]

我在解释这个错误的实际含义时遇到了一些问题,显然 one person's solution 涉及将 POSIXct 类转换为字符,这在我的情况下实际上不起作用。

任何人都可以阐明如何进行这项工作吗?我也对其他方法持开放态度,我只是喜欢 ddply 语法的相对简单。我将在不久的将来处理更多基于时间的数据,因此我希望有人能提供有关如何使用其他基于 R 的工具来解决此类聚合问题的见解。

【问题讨论】:

  • 我编辑了 plyr 语句,因为它似乎引用了示例中没有的数据和变量

标签: r plyr


【解决方案1】:

我使用str 检查了您的数据,结果发现您的日期实际上是因素。您可以使用lubridate 让他们约会:

library(lubridate)
test.data2 <- transform(test.data,lst = dmy_hm(login.sessn.ts))

ddply(test.data2, c("user.nm"), summarize, 
      change.count=sum(ip.addr.txt),
      max.change.time=max(lst),
      min.change.time=min(lst))

  user.nm change.count     max.change.time     min.change.time
1       a            3 2013-11-03 13:55:00 2013-01-06 12:03:44
2       b            3 2013-01-06 08:35:32 2013-01-06 08:35:32
3       c            4 2013-01-11 10:16:00 2013-01-11 10:16:00
4       d           10 2046-11-24 13:24:29 2013-01-12 11:08:04

【讨论】:

  • 不幸的是,lubridate 不适用于我的 R (2.15.2) 版本。另外,这是我对示例数据的错误——我很快就会发布新数据。在我的完整数据集上,该类绝对是 POSIXct
猜你喜欢
  • 2011-05-07
  • 2012-02-08
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多