如何在 R 中从频数表中求得均值和标准差?
要从频数表中求得均值和标准差,我们需要应用均值和标准差的公式用于频数数据。例如,如果有一个名为 df 的数据框架,其中包含一个 x 列用于表示单位和一个 frequency 列用于表示计数,那么均值和标准差可以这样计算:
Mean = sum(df$x*df$frequency)/sum(df$frequency) SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.
示例 1
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
示例 2
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
广告