How to find mean and standard deviation from frequency table in R?

R ProgrammingServer Side ProgrammingProgramming

To find the mean and standard deviation from frequency table, we would need to apply the formula for mean and standard deviation for frequency data. For example, if we have a data frame called df that contains a column x for units and frequency for counts then the mean and standard deviation can be calculated as −

Mean = sum(df$x*df$frequency)/sum(df$frequency)
SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.

Example1

 Live Demo

x<−rpois(20,5)
frequency<−sample(1:100,20)
df1<−data.frame(x,frequency)
df1

Output

 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

Example

Mean=sum(df1$x*df1$frequency)/sum(df1$frequency)
Mean

Output

[1] 6.55288

Example

SD=sqrt(sum((df1$x−Mean)**2*df1$frequency)/(sum(df1$frequency)−1))
SD

Output

[1] 4.172396

Example2

 Live Demo

y<−rnorm(20,25,3.24)
frequency<−rpois(20,10)
df2<−data.frame(y,frequency)
df2

Output

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

Example

Mean=sum(df2$y*df2$frequency)/sum(df2$frequency)
Mean

Output

[1] 24.58724

Example

SD=sqrt(sum((df2$y−Mean)**2*df2$frequency)/(sum(df2$frequency)−1))
SD

Output

[1] 2.284964
raja
Published on 09-Feb-2021 12:34:54
Advertisements