Wanted to count the occurrence of a variable in a data frame. It’s easy to do in Excel PivotTable and now learning how to do it in R.
v_time visitor
1/2/2018 16:07 Jack
1/3/2018 16:09 Jack
1/3/2018 16:12 David
1/3/2018 16:16 Kate
1/2/2018 16:21 David
1/2/2018 16:32 Jack
1/4/2018 16:33 Kate
1/4/2018 16:55 Jack
Excel can make it easily like this:
I’ve tried some lines but still not getting there.
visitor <- c("Jack", "Jack", "David", "Kate", "David", "Jack", "Kate", "Jack")
v_time <- c("1/2/2018 16:07","1/3/2018 16:09","1/3/2018 16:12","1/3/2018 16:16","1/2/2018 16:21","1/2/2018 16:32","1/4/2018 16:33", "1/4/2018 16:55")
df <- data.frame(v_time, visitor)
as.Date(as.POSIXct(df$v_time, "%m/%d/%Y"))
library(plyr)
count(df$visitor, 'v_time')
as.data.frame(table(df$visitor))
What’s the way to produce the Excel PivotTable alike output in R? Thank you.