Nick
Nick

Reputation: 45

R spread and aggregate

I have a data.frame, df:

userId action    countEvent
A       click        1   
A       view         2
A       purchase     1 
B       Edit         1
B       Edit         1
C       click        1

How can I can get this data.frame

userId  click view purchase Edit
A         1     2      1     0
B         0     0      0     2 
C         1     0      0     0

im try

spred_data<- df %>%  spread(action    ,countEvent )

but its return error

Duplicate identifiers for rows

Upvotes: 0

Views: 266

Answers (1)

Kumar Manglam
Kumar Manglam

Reputation: 2832

You can easily do this using data.table package. Data.table package is very fast compared to data.frame(thats why nowadays I for data munging, I use mainly data.table). Below is the solution using data.table package:

dcast(df[,sum(countEvent), by=.(userId, action)], 
         formula = userId ~  action, 
         fun.aggregate=sum, 
         value.var="V1")

Upvotes: 1

Related Questions