user3849475
user3849475

Reputation: 339

calculate the rate under the same in using R

I have a question to calculate the rate under the same id numbers. Here is the sample dataset 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

The ideal output is

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

Thanks.

Upvotes: 2

Views: 72

Answers (2)

akrun
akrun

Reputation: 887068

Try

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

Or

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

data

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))

Upvotes: 2

Jilber Urbina
Jilber Urbina

Reputation: 61154

Just for fun, you can use 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

Upvotes: 3

Related Questions