使用R计算相同的速率

calculate the rate under the same in using R

我有一个问题要计算相同身份证号码下的费率。 这是示例数据集 d:

id answer
1   1
1   0
1   0
1   1
1   1
1   1
1   0
2   0
2   0
2   0
3   1
3   0

理想的输出是

id  rate          freq
1   4/7 (=0.5714)  7
2   0              3
3   1/2 (=0.5)     2

谢谢。

尝试

library(data.table)
setDT(df1)[,list(rate= mean(answer), freq=.N) ,id]
#   id      rate freq
#1:  1 0.5714286    7
#2:  2 0.0000000    3
#3:  3 0.5000000    2

或者

library(dplyr)
 df1 %>% 
    group_by(id) %>%
    summarise(rate=mean(answer), freq=n())

数据

df1 <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L), answer = c(1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 
0L)), .Names = c("id", "answer"), class = "data.frame", 
row.names = c(NA, -12L))

为了好玩,您可以使用aggregate

> aggregate(answer~id, function(x) c(rate=mean(x), freq=length(x)), data=df1)
  id answer.rate answer.freq
1  1   0.5714286   7.0000000
2  2   0.0000000   3.0000000
3  3   0.5000000   2.0000000