想要计算数据框中变量的出现次数。在Excel数据透视表中可以轻松完成,现在可以在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可以很容易地做到这一点:
我已尝试过一些线路,但仍然没有到达那里。
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))
在R中生成Excel数据透视表相同输出的方法是什么?谢谢。