# How to calculate mean, median, mode, std dev from distribution

How to calculate mean, variance, median, standard deviation and modus from distribution? If I randomly generate numbers which forms the normal distribution I've specified the mean as m=24.2 standard deviation as sd=2.2:

> dist = rnorm(n=1000, m=24.2, sd=2.2)


Then I can do following:

Mean:

> mean(dist)
[1] 24.17485


Variance:

> var(dist)
[1] 4.863573


Median:

> median(dist)
[1] 24.12578


Standard deviation:

> sqrt(var(dist))
[1] 2.205351


Mode aka Modus (taken from here):

> names(sort(-table(dist)))[1]
[1] "17.5788181686221"

1. Is this the whole magic, or is there something else that I did not realized?
2. Can I somehow visualize my bell shaped normal distribution with vertical lines representing (mean, median...)?
3. What does those attributes say about distribution?

PS: code is in R

• It's not clear what you mean by your question 1. Question 3 seems too broad as it stands, since one could write a lot about each. Could you review the wikipedia pages on the mean, median, standard deviation and mode, and then ask something more specific? Jun 19, 2015 at 2:37
• Also, your title speaks of a "distribution" but your body text seems to be referring instead to a sample. Should your title refer to a sample? Jun 19, 2015 at 8:53
• @Glen_b by 1st I mean that if the methods that I've posted are OK for calculating parameters (mean, variance ...) for distribution or something else or somehow differently should be used? 3rd I did not realize that is too broad, I was expecting answer like: if median is larger than mean then your data .... Regarding retitling maybe it is OK, indeed I was working with normal distribution or am I wrong? Jun 19, 2015 at 23:23
• I'm sorry but I can't follow your comment there at all. Jun 20, 2015 at 10:32
• You don't need to bin a continuous variable before you can estimate its mode. See stats.stackexchange.com/questions/176112/… for discussion. (You should want to avoid binning as being dependent on arbitrary choices of bin width and origin.) Aug 17, 2019 at 18:46

First a general comment on the mode:

You should not use that approach to get the mode of (at least notionally) continuously distributed data; you're unlikely to have any repeated values (unless you have truly huge samples it would be a minor miracle, and even then various numeric issues could make it behave in somewhat unexpected ways), and you'll generally just get the minimum value that way. It would be one way to find one of the global modes in discrete or categorical data, but I probably wouldn't do it that way even then. Here are several other approaches to get the mode for discrete or categorical data:

x = rpois(30,12.3)

tail(sort(table(x)),1)   #1: category and count; if multimodal this only gives one

w=table(x); w[max(w)==w] #2: category and count; this can find more than one mode

which.max(table(x))      #3: category and *position in table*; only finds one mode


If you just want the value and not the count or position, names() will get it from those

To identify modes (there can be more than one local mode) for continuous data in a basic fashion, you could bin the data (as with a histogram) or you could smooth it (using density for example) and attempt to find one or more modes that way.

Fewer histogram bins will make your estimate of a mode less subject to noise, but the location won't be pinned down to better than the bin-width (i.e. you only get an interval). More bins may allow more precision within a bin, but noise may make it jump around across many such bins; a small change in bin-origin or bin width may produce relatively large changes in mode. (There's the same bias-variance tradeoff all over statistics.)

Note that summary will give you several basic statistics.

[You should use sd(x) rather than sqrt(var(x)); it's clearer for one thing]

--

In respect of q.2 yes; you could certainly show mean and median of the data on a display such as a histogram or a box plot. See here for some examples and code that you should be able to generalize to whatever cases you need.

• Thank you, can you please explain the difference between binding and smoothing? Jun 19, 2015 at 23:37
• @Wakan What's binding? Jun 20, 2015 at 10:32
• Extremely useful. Very surprising the R Project didn't make this into a base function. Dec 10, 2015 at 16:40

Some additional and not very well known descriptive statistics.

x<-rnorm(10)

sd(x) #Standard deviation

fivenum(x) #Tukey's five number summary, usefull for boxplots

IQR(x) #Interquartile range

quantile(x) #Compute sample quantiles

range(x) # Get minimum and maximum


I am sure you can find many others in one of those freely available R manuals.

• Can you post some situations when you've used those (fivenum, IQR, quantile ...) and they were useful? Jun 20, 2015 at 7:18

As @Glen_b described the mode of a continuous distribution is not as straightforward as it is for a vector of integers.

This R code will get the mode for a continuous distribution, using the incredibly useful hist() function from base R. As @Glen_b described this involves putting observations into bins - discrete categories where if the observation falls within the bin interval it is counted as an instance of that bin, which gets around the problem of it being highly unlikely in a continuous distribution to observe the exact same value twice.

set.seed(123)
dist <- rnorm(n=1000, m=24.2, sd=2.2)
h <- hist(dist, # vector
plot = F, # stops hist() from automatically plotting histogram
breaks = 40) # number of bins


Now we treat the midpoint of the bin interval that has the maximum count within it as the mode

h$$mids[which.max(h$$counts)]

# [1] 23.75


Voila! The mode.

p.s. you could also treat the start of the interval as the mode via h$breaks[which.max(h$counts)]. As discussed modes for continuous distributions are not simple and require decisions to be made, hence why there is no simple function for them like there is with mean() and median()

• So this boils down to saying that to find the mode you find the most frequently occurring bin. What do you do when there are ties? How do you choose bins systematically? Aug 17, 2019 at 18:49
• These are very good questions @Nick Cox? Would love to know the answer. I guess you would need a rule for when there are ties. Aug 19, 2019 at 4:50