1
我有這樣一個數據幀的類型,計算類型計數,並添加用逗號分隔的data.table
ID <- c("ID001","ID001","ID001","ID002","ID002","ID002")
ToolID <- c("SWP","SWP","SWP","ISP","ISP","ISP")
Type <- c("A","B","C","D","E","A")
WHEN <- c("2017-08-15 12:44:11","2017-08-15 12:44:11","2017-08-14 19:07:11",
"2017-08-17 11:24:15","2017-08-17 11:24:15","2017-08-17 11:24:15")
df <- data.frame(ID,ToolID,Type,WHEN)
df$WHEN <- as.POSIXct(df$WHEN,format="%Y-%m-%d %H:%M:%S")
我試圖把所有類型的用逗號分隔的一列,並且還計算計數對於ID,按照(Tool_ID & ID)進行分組,同時僅取最大值(WHEN),即相應ID的最近時間戳。
我所需的輸出是
ID ToolID Type Type_count WHEN
ID001 SWP A,B 2 2017-08-15 12:44:11
ID002 ISP D,E,A 3 2017-08-17 11:24:15
我嘗試使用data.table而且做得這樣
library(data.table)
setDT(df)[, WHEN := as.POSIXct(WHEN)]
df1 <- df[, max(WHEN), by = list(ID,ToolID)]
colnames(df1)[which(names(df1) == "V1")] <- "WHEN"
如何獲得的類型和種類數增加DF1讓我期望的輸出? 有人能指出我在正確的方向嗎?