我之前問過類似的問題並得到了很好的答案,但需要關於總結和日期主題的更多指導。 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