使用汇总函数

时间:2016-04-17 15:03:10

标签: r max aggregation mean rollup

我有一个包含子序列(行组)的数据框 并且识别这些子序列的条件是观察列差异的激增。这就是数据的样子:

> dput(test)
structure(list(vid = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    .Label = "2a38ebc2-dd97-43c8-9726-59c247854df5", class = "factor"), 
    events = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 
    2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("click", 
    "mousedown", "mousemove", "mouseup"), class = "factor"), 
    deltas = structure(6:25, .Label = c("154875", "154878", "154880", 
    "155866", "155870", "38479", "38488", "38492", "38775", "45595", 
    "45602", "45606", "45987", "50280", "50285", "50288", "50646", 
    "54995", "55001", "55005", "55317", "59528", "59533", "59537", 
    "59921", "63392", "63403", "63408", "63822", "66706", "66710", 
    "66716", "67002", "73750", "73755", "73759", "74158", "77999", 
    "78003", "78006", "78076", "81360", "81367", "81371", "82381", 
    "93365", "93370", "93374", "93872"), class = "factor"), 
    serial = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
    19, 20), diff = c(0, 9, 4, 283, 6820, 7, 4, 381, 4293, 5, 3, 358, 4349, 6, 4,
    312, 4211, 5, 4, 384)), 
    .Names = c("vid", "events", "deltas", "serial", "diff"),
    row.names = c(NA, 20L), class = "data.frame")

我正在尝试添加一个列,该列将指示何时识别新子序列并为整个子序列分配唯一ID。我将通过以下示例演示分组的标准:
第5行的diff值为6829,比该行(283)的最大值高10倍。 结果应该是这样的df:

structure(list(vid = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    .Label = "2a38ebc2-dd97-43c8-9726-59c247854df5", class = "factor"), 
    events = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 
    2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("click", 
    "mousedown", "mousemove", "mouseup"), class = "factor"), 
    deltas = structure(6:25, .Label = c("154875", "154878", "154880", 
    "155866", "155870", "38479", "38488", "38492", "38775", "45595", 
    "45602", "45606", "45987", "50280", "50285", "50288", "50646", 
    "54995", "55001", "55005", "55317", "59528", "59533", "59537", 
    "59921", "63392", "63403", "63408", "63822", "66706", "66710", 
    "66716", "67002", "73750", "73755", "73759", "74158", "77999", 
    "78003", "78006", "78076", "81360", "81367", "81371", "82381", 
    "93365", "93370", "93374", "93872"), class = "factor"), serial = c(1, 
    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
    19, 20), 
    diff = c(0, 9, 4, 283, 6820, 7, 4, 381, 4293, 5, 
    3, 358, 4349, 6, 4, 312, 4211, 5, 4, 384), 
    group = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5)), 
    .Names =  c("vid", "events", "deltas", "serial", "diff", "group"), 
    row.names = c(NA, 20L), class = "data.frame")

任何帮助非常感谢

2 个答案:

答案 0 :(得分:0)

用户Gopala的礼貌: 怎么样df $ group< - cumsum(df $ diff> 500)+ 1(你指定的任何标准)。 - Gopala 31分钟前

答案 1 :(得分:0)

让我更详细地介绍一下它的工作原理及其工作原理。

首先,让我们添加一个没有cumsum部分的列:

df$tag <- df$diff > 500
head(df)
                                   vid    events deltas serial diff   tag
1 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  38479      1    0 FALSE
2 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  38488      2    9 FALSE
3 2a38ebc2-dd97-43c8-9726-59c247854df5   mouseup  38492      3    4 FALSE
4 2a38ebc2-dd97-43c8-9726-59c247854df5     click  38775      4  283 FALSE
5 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  45595      5 6820  TRUE
6 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  45602      6    7 FALSE

正如您所看到的,它只是在标记列中创建了一个TRUE / FALSE值的逻辑,表示差异是否足够大&#39; (根据选定的门槛值)。

现在,当您对该列进行cumsum并将其存储在group列中时,它将继续累积添加。每个TRUE值都会将累积和增加1,每个FALSE值将使累积和保持与该行被击中之前相同。

因此,这将为您提供所需的递增group值:

df$group <- cumsum(df$tag)
head(df)
                                   vid    events deltas serial diff   tag group
1 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  38479      1    0 FALSE     0
2 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  38488      2    9 FALSE     0
3 2a38ebc2-dd97-43c8-9726-59c247854df5   mouseup  38492      3    4 FALSE     0
4 2a38ebc2-dd97-43c8-9726-59c247854df5     click  38775      4  283 FALSE     0
5 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  45595      5 6820  TRUE     1
6 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  45602      6    7 FALSE     1

请注意,组值从零开始。由于前几个FALSE值的累积和为零。但是,您可能希望组标识符以1开头。所以,我在cumsum添加了一个1,但您也可以按照以下步骤添加一个步骤。

df$group <- df$group + 1
head(df)
                                   vid    events deltas serial diff   tag group
1 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  38479      1    0 FALSE     1
2 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  38488      2    9 FALSE     1
3 2a38ebc2-dd97-43c8-9726-59c247854df5   mouseup  38492      3    4 FALSE     1
4 2a38ebc2-dd97-43c8-9726-59c247854df5     click  38775      4  283 FALSE     1
5 2a38ebc2-dd97-43c8-9726-59c247854df5 mousemove  45595      5 6820  TRUE     2
6 2a38ebc2-dd97-43c8-9726-59c247854df5 mousedown  45602      6    7 FALSE     2

希望这有帮助。