How can I calculate Standard Deviation, step-by-step, in R? Thanks!
closed as off-topic by John, shadowtalker, Christoph Hanck, kjetil b halvorsen, gung♦ Sep 11 '15 at 6:58
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – shadowtalker, Christoph Hanck, kjetil b halvorsen, gung
> a <- c(179,160,136,227) > sd(a)  38.57892 > sqrt(sum((a-mean(a))^2/(length(a)-1)))  38.57892
So, you want to calculate the standard deviation step-by-step. So, firstly, you should calculate the sum of the differences of all data points with the mean.
Have a variable called
count and set it to the value 0.
For that, you loop through the data set with a variable, say
i and subtract
i every time with the mean. Mean can be calculated as
Add the result of every loop iteration to count, by
count = count + (i-mean)^2
Now, divide the count variable by
len(dataset) - 1
The result is the variance. So, for calculating the standard deviation, you have to square root the above value.
In R, you do this as:
Finally, the result you get after applying the square root is the Standard Deviation.