• R语言
  • 请问这种情况应该怎么处理(实在不知道怎么描述)

ID record
1 yes
1 yes
1 yes
2 yes
2 yes
3 yes

怎样处理成

ID Times
1 3
2 2
3 1

数据量四万多条,ID两万多……
library(dplyr)
set.seed(1)
Df=data.frame(ID=sample(1:20000,40000,TRUE),
record=sample(c('yes','no'),40000,TRUE))
summarise(group_by(Df, ID,record), 'count of record' = n())
只用base包就好了。
Df=data.frame(ID=sample(1:20,40000,TRUE),
record=sample(c('yes','no'),40000,TRUE))
aggregate(Df$record=="yes",by=list(Df$ID), FUN=sum)
不知道record是否考虑,不考虑,直接用table函数。
19 天 后
用plyr包里面的count函数也行
library(plyr)
a  # your data.frame
b = count(a,vars = "names")
names(b) <- c("names","Times")
b
aa <- data.frame(ID = c(1,1,1,2,2,2,3,3,3),rec = rep('yes',9))
table(aa)