I have data like this:
Id Action timestamp
1 click #########
1 view #########
1 data #########
2 click #########
2 click #########
I want to convert the categories into columns containing frequency(counts) like this:
id click view data
1 1 1 1
2 2 0 0
How can I do this? Thanks !
Here is how you can do it.
# create the data frame
df <- data.frame(Id=c(1,1,1,2,2), Action=c("click", "view", "data", "click", "click"))
df
#> Id Action
#> 1 1 click
#> 2 1 view
#> 3 1 data
#> 4 2 click
#> 5 2 click
# Use reshape2::dcast
library(reshape2)
dcast(df, Id ~ Action, fun.aggregate = length)
#> Id click data view
#> 1 1 1 1 1
#> 2 2 2 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With