要从频率表中找到平均值和标准偏差,我们需要将公式用于频率数据的平均值和标准偏差。例如,如果我们有一个名为df的数据框,其中包含一列x表示单位,频率则表示计数,则均值和标准差可以计算为-
Mean = sum(df$x*df$frequency)/sum(df$frequency) SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.
x<−rpois(20,5) frequency<−sample(1:100,20) df1<−data.frame(x,frequency) df1输出结果
x frequency 1 6 4 2 7 26 3 1 86 4 2 6 5 4 52 6 4 61 7 1 55 8 4 23 9 8 38 10 3 40 11 8 54 12 10 56 13 7 74 14 9 70 15 7 59 16 16 95 17 4 20 18 5 9 19 4 82 20 9 45
Mean=sum(df1$x*df1$frequency)/sum(df1$frequency) Mean输出结果
[1] 6.55288
SD=sqrt(sum((df1$x−Mean)**2*df1$frequency)/(sum(df1$frequency)−1)) SD输出结果
[1] 4.172396
y<−rnorm(20,25,3.24) frequency<−rpois(20,10) df2<−data.frame(y,frequency) df2输出结果
y frequency 1 27.44960 7 2 25.80343 5 3 22.64088 8 4 22.39061 7 5 24.55087 7 6 24.41826 16 7 23.24647 11 8 25.61511 9 9 22.42244 11 10 26.77522 14 11 21.89209 11 12 22.95852 8 13 25.79808 16 14 22.39654 6 15 21.20728 12 16 28.25911 17 17 26.67983 8 18 25.24964 12 19 20.92070 8 20 28.25806 7
Mean=sum(df2$y*df2$frequency)/sum(df2$frequency) Mean输出结果
[1] 24.58724
SD=sqrt(sum((df2$y−Mean)**2*df2$frequency)/(sum(df2$frequency)−1)) SD输出结果
[1] 2.284964