r - calc mean/std/ci from column -
is there package calculate each specific n number, mean/std/ci. in example starting data:
> n = c(0,0,0,0,0,0,0,2,2,2,2,5,5,5,5,8,8,8,8) > s = c(43,23,65,43,12,54,43,12,2,43,62,25,55,75,95,28,48,68,18) > df = data.frame(n, s) > df n s 1 0 43 2 0 23 3 0 65 4 0 43 5 0 12 6 0 54 7 0 43 8 2 12 9 2 2 10 2 43 11 2 62 12 5 25 13 5 55 14 5 75 15 5 95 16 8 28 17 8 48 18 8 68 19 8 18
resulting as:
data n mean std ci 0 40 .. .. 2 30 .. .. 5 63 .. .. 8 41 .. ..
you can use dplyr
package.
here's code snippet. note, i'm assuming want build confidence interval using standard normal approximation @ 95% level can make whatever choice like.
n = c(0,0,0,0,0,0,0,2,2,2,2,5,5,5,5,8,8,8,8) s = c(43,23,65,43,12,54,43,12,2,43,62,25,55,75,95,28,48,68,18) df = data.frame(n, s) df %>% group_by(n) %>% summarise(mean = mean(s), std = sqrt(var(s)), lower = mean(s) - qnorm(.975)*std/sqrt(n()), upper = mean(s) + qnorm(.975)*std/sqrt(n())) source: local data frame [4 x 5] n mean std lower upper 1 0 40.42857 17.88721 27.177782 53.67936 2 2 29.75000 27.69326 2.611104 56.88890 3 5 62.50000 29.86079 33.236965 91.76303 4 8 40.50000 22.17356 18.770313 62.22969
Comments
Post a Comment