2017-08-04 49 views
0

感謝其他用戶的幫助,我成功地將我的數據集劃分爲序列並聚合每個序列的響應。一個序列由刺激的發生(A或B)定義[在用戶發生的任何一種刺激之前,它就是所謂的0序列]。這意味着每個用戶可能根據他感知的刺激的量有多個序列。每個用戶都有事件日誌,並根據上述條件分割事件日誌。我用下面的代碼:將事件日誌序列拆分並聚合到區間中

#change the date into posixct format 
df$Date <- as.POSIXct(strptime(master$Date,"%d.%m.%Y %H:%M")) 

#arrange the dataframe according to User and Date 
df <- arrange(df, User,Date) 

#create a unique ID for each stimuli combination 
df$stims <- with(df, paste(cumsum(StimuliA), cumsum(StimuliB), sep="_")) 

#aggregate all the eventlog rows according to the stimuli IDs 
df1 <- aggregate(. ~ User + stims, data=df, sum) 

來源:Summarize and count data in R with dplyr

數據集:

structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), Date = c("02.12.2015 20:16", "03.12.2015 20:17", 
"02.12.2015 20:44", "03.12.2015 09:32", "03.12.2015 09:33", "07.12.2015 08:18", 
"08.12.2015 19:40", "08.12.2015 19:43", "22.12.2015 18:22", "22.12.2015 18:23", 
"23.12.2015 14:18", "05.01.2016 11:35", "05.01.2016 13:21", "05.01.2016 13:22", 
"05.01.2016 13:22", "04.08.2016 08:25"), StimuliA = c(0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), StimuliB = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L), 
    R2 = c(1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 
    0L, 0L, 0L), R3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 0L, 1L, 0L), R4 = c(0L, 0L, 0L, 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, 0L, 0L, 0L), R6 = c(0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L 
    ), R7 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 1L, 0L, 0L), User_Seq = c("1_0_0", "1_0_0", "1_0_0", 
    "1_0_0", "1_0_0", "1_1_0", "1_1_0", "1_1_0", "1_1_0", "1_1_0", 
    "1_2_0", "1_2_1", "1_2_1", "1_2_1", "1_2_1", "1_2_2")), .Names = c("User", 
"Date", "StimuliA", "StimuliB", "R2", "R3", "R4", "R5", "R6", 
"R7", "User_Seq"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-16L), 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")), User_Seq = structure(list(), class = c("collector_character", 
"collector"))), .Names = c("User", "Date", "StimuliA", "StimuliB", 
"R2", "R3", "R4", "R5", "R6", "R7", "User_Seq")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec")) 

我的目標是適應這個代碼來創建序列相同的摘要,但將回答分成兩部分。一個在刺激日期後的第一週,然後按照該順序彙總所有其他「滯後」響應。

我在下面的例子中說明了這一點。也可以用長格式來做這件事,並增加一列,用1/0和相同的日期標識滯後響應,但最佳輸出是以寬格式。

User Da   StimuliA StimuliB Seq_ID R2 R3 R4 R5 R6 R7 R2l R3l R4l R5l R6l R7l 
1 02.12.2015 20:16 0  0 1_0_0  4 0 0 0 1 0 0 0 0 0 0 0 
1 07.12.2015 08:18 1  0 1_1_0 1 0 0 0 0 1 2 0 0 0 0 0 
1 23.12.2015 14:18 1  0 1_2_0 0 0 0 0 0 0 0 0 0 0 0 0 
1 05.01.2016 11:35 0  1 1_2_1 0 2 0 0 0 1 0 1 0 0 0 0 
1 04.08.2016 08:25 0  1 1_2_2 0 0 0 0 0 0 0 0 0 0 0 0 

f.e正如你可以在這裏看到在R2L被聚合的樣品行9 & 10(Resoibse 2落後),因爲他們在2015年7月12日08:18後一週發生。

+0

你想如何以及何時聚合這些結果? 你說過當某個刺激A或刺激B發生時,你想總結一切......然後(我猜)在刺激發生之後的一週(或7天)內對所有Ri列求和,就是正確?那麼爲什麼你最後一個例子中的第一行呢?爲什麼日期05.01.2016的聚合版本中R3不等於2? – zwep

+0

是的,但每個用戶已經在平臺上並正在執行操作。這就是爲什麼每個用戶有1行,其中第一個刺激發生之前的所有反應都被彙總。如果我使用與以前相同的代碼,則將使用stims ID 0_0。關於R3,我更新了這個,對不起,我手工彙總了它,併發了一個錯字。 – svnnf

+0

我不能想出一個漂亮而美麗的解決方案,儘管...我最好的概念是使用data.table,檢查刺激設置爲1的位置,獲取這些行的日期... add 7天......然後根據這些值對錶格進行分片和聚合。 – zwep

回答

0

我找到了我的問題的解決方案。基本上我通過序列號(Seqid)和日期來組織它,並將它分組爲seqid。然後我創建一個7天后的最短日期的新列。之後,簡單地比較這個最早的日期和每個正常日期的7天,並將第一週的值設爲0,其他日期設爲1。

df <- df %>% 
     arrange(seqid, Date) %>% 
     group_by(seqid) %>% 
     mutate(Date7 = (min(Date) + 604800)) %>% 
     mutate(Group = ifelse(Date7>Date,0,1)) 

之後,簡單地把它重塑了廣泛的格式如你問題。