我之前问过类似的问题并得到了很好的答案,但需要关于总结和日期主题的更多指导。 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
是否缺少从'用户连续== 2'?一个没有刺激... – Sotos
@Sotos是的!你是对的,我忘了那个。 – svnnf