1
我掙扎使用dplyr和tidyr採取DF這種形式:dplyr tidyr擴大和總結特定的列
myDf <- data.frame(id = c(1,1,1,1,2,2),
event = c('a','b','a','b','a','b'),
a_property = c(1,NA,2, NA, 3, NA),
b_property = c(NA,2,NA, 3, NA, 4))
> myDf
id event a_property b_property
1 a 1 NA
1 b NA 2
1 a 2 NA
1 b NA 3
2 a 3 NA
2 b NA 4
,並轉換成以下所需的格式:
id count_event_a count_event_b sum_property_a sum_property_b
1 2 2 3 5
2 1 1 5 4
做兩個步驟。重塑像這樣的問題:http://stackoverflow.com/questions/20620492/reshape-long-to-wide-with-multiple-groupings然後總結()得到計數/總和。 – MrFlick