如何从R中的频率表中找到平均值和标准偏差?
要从频率表中找到平均值和标准偏差,我们需要将公式用于频率数据的平均值和标准偏差。例如,如果我们有一个名为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.
例1
x<−rpois(20,5)输出结果frequency<−sample(1:100,20)
df1<−data.frame(x,frequency)
df1
x frequency1 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 frequency1 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
以上是 如何从R中的频率表中找到平均值和标准偏差? 的全部内容, 来源链接: utcz.com/z/352221.html