2017-07-26 90 views
1

我之前问过类似的问题并得到了很好的答案,但需要关于总结和日期主题的更多指导。 Summarize and count data in R with dplyr总结数据并保留日期列值

目标:

在我的新的数据集我有日期栏,当事件发生。当我想在本例中继续在其他岗位的建议,我得到一个错误信息:

数据集:

structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), 
Date = c("25.11.2015 13:59", "03.12.2015 09:32", "07.12.2015 08:18", "08.12.2015 19:40", "08.12.2015 19:40", 
"22.12.2015 08:50", "22.12.2015 08:52", "05.01.2016 13:22", 
"06.01.2016 09:18", "14.02.2016 22:47", 
"20.02.2016 21:27", "01.04.2016 13:52", "24.07.2016 07:03"), 
    StimuliA = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L), StimuliB = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
    1L, 0L, 0L, 0L), R2 = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L, 1L, 0L), R3 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L), R4 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), R5 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), R6 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 
    0L, 0L, 0L, 0L), R7 = c(0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L), stims = c("0_0", "0_0", "1_0", "1_0", "1_1", 
    "1_1", "1_1", "1_1", "1_1", "1_2", "1_2", "1_2", "2_2")), .Names = c("User", "Date", "StimuliA", "StimuliB", "R2", "R3", "R4", "R5", "R6", "R7", "stims"), row.names = c(NA, -13L), spec = structure(list(
    cols = structure(list(User = structure(list(), class = c("collector_integer", 
    "collector")), Date = structure(list(), class = c("collector_character", 
    "collector")), StimuliA = structure(list(), class = c("collector_integer", 
    "collector")), StimuliB = structure(list(), class = c("collector_integer", 
    "collector")), R2 = structure(list(), class = c("collector_integer", 
    "collector")), R3 = structure(list(), class = c("collector_integer", 
    "collector")), R4 = structure(list(), class = c("collector_integer", 
    "collector")), R5 = structure(list(), class = c("collector_integer", 
    "collector")), R6 = structure(list(), class = c("collector_integer", 
    "collector")), R7 = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("User", "Date", "StimuliA", "StimuliB", 
    "R2", "R3", "R4", "R5", "R6", "R7")), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df", "tbl", "data.frame")) 

代码:

df$stims <- with(df, paste(cumsum(StimuliA), cumsum(StimuliB), sep="_"))  
aggregate(. ~ User + stims, data=df, sum) 
Error in Summary.factor(c(12L, 2L), na.rm = FALSE) : 
‘sum’ not meaningful for factors 

问题/预期结果: 在我的结果中,我想保留刺激发生的日期(或刺激A和B是0,则特定用户的第一个日期)

User Date   StimuliA StimuliB R2 R3 R4 R5 R6 R7 
1 25.11.2015 13:59  0   0  1 0 0 0 0 1 
1 07.12.2015 08:18  1   0  0 0 0 0 1 0 
1 08.12.2015 19:40  0   1  0 2 0 0 1 1 
2 05.01.2016 13:22  0   0  0 0 0 0 1 0 
2 14.02.2016 22:47  0   1  2 0 0 0 0 0 
2 24.07.2016 07:03  1   0  0 0 0 0 0 0 

在这个结果表的,我们有值(R2-R7)中,当刺激A和B是仍为0 [线路1]接着的总和对于每种刺激,都有R2-R7的总和,直到下一次刺激发生。

此建议在以前的帖子,但我无法使其工作:

你不想日期为因素的工作。使用as.Date将日期变换为 Date变量(SO上的许多帖子)。一种方法 然后将单独聚合日期变量的用户和 stims类似于上面,取最小值而不是总和。然后合并 两个生成的data.frames。如果这没有意义,那么可能 值得问一个新的问题,链接到这个问题,添加日期变量的其他问题 。还包括一个例子 数据集,其包括经由dplyr此变量@lmo

+0

是否缺少从'用户连续== 2'?一个没有刺激... – Sotos

+0

@Sotos是的!你是对的,我忘了那个。 – svnnf

回答

1
一个

想法是将过滤所有非刺激和抓住每个用户的第一观察(通过slice)。该过滤器的所有的刺激和bind_rows,即

library(dplyr) 

bind_rows(
    df %>% 
    filter(rowSums(.[3:4]) == 0) %>% 
    group_by(User) %>% 
    slice(1L), 
    df %>% 
    filter(rowSums(.[3:4]) != 0)) %>% 
    arrange(User) 

其中给出,

# A tibble: 6 x 11 
# Groups: User [2] 
    User    Date StimuliA StimuliB R2 R3 R4 R5 R6 R7 stims 
    <int>   <chr> <int> <int> <int> <int> <int> <int> <int> <int> <chr> 
1  1 25.11.2015 13:59  0  0  1  0  0  0  0  0 0_0 
2  1 07.12.2015 08:18  1  0  0  0  0  0  0  0 1_0 
3  1 08.12.2015 19:40  0  1  0  0  0  0  0  0 1_1 
4  2 05.01.2016 13:22  0  0  0  0  0  0  1  0 1_1 
5  2 14.02.2016 22:47  0  1  0  0  0  0  0  0 1_2 
6  2 24.07.2016 07:03  1  0  0  0  0  0  0  0 2_2 
+0

谢谢@Sotos!在你发布的tibble中,R2-R7中的值没有被汇总,据我所知。您的代码是否发布了您在会话中运行的所有内容,或者是否插入了其他内容?你在哪里创建了这个stims? (像在之前的文章中那样) – svnnf

+0

@svnnf此时,您可以将第一列,第二列和最后一列合并(加入)上一篇文章的data.frame,并使用第一列和最后一列作为ID变量。 – lmo

+0

@svnnf包含在你的例子中的'stims'。至于'R2-R7' ...你如何得到他们的价值? (我刚刚看到它们不同) – Sotos

2

这里,Date制成POSIXct类,保存日期时间,这是这项任务至关重要。 as.Date()将从日期中删除时间。

library(dplyr) 

union_all(
    df %>% 
     mutate(Date = as.POSIXct(strptime(Date, "%d.%m.%Y %H:%M"))) %>% 
     filter(StimuliA == 0, StimuliB == 0, Date == min(Date)), 
    df %>% 
     mutate(Date = as.POSIXct(strptime(Date, "%d.%m.%Y %H:%M"))) %>% 
     filter(StimuliA == 1 | StimuliB == 1)) %>% 
    arrange(User, Date) %>% 
    select(-stims) 

输出:

 User    Date StimuliA StimuliB R2 R3 R4 R5 R6 R7 
    <int>    <dttm> <int> <int> <int> <int> <int> <int> <int> <int> 
    1  1 2015-11-25 13:59:00  0  0  1  0  0  0  0  0 
    2  1 2015-12-07 08:18:00  1  0  0  0  0  0  0  0 
    3  1 2015-12-08 19:40:00  0  1  0  0  0  0  0  0 
    4  2 2016-02-14 22:47:00  0  1  0  0  0  0  0  0 
    5  2 2016-07-24 07:03:00  1  0  0  0  0  0  0  0 
+0

很好地完成了这个! – akrun