【发布时间】: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 语句,因为它似乎引用了示例中没有的数据和变量