Zack
Zack

Reputation: 1

How do I find proportion of the datapoints (within one column) that are within one standard deviation away from the mean in R code?

I was given the mean: 62.4 and the standard deviation: 18.7. So given the mean and standard deviation, how do I find out the data points that are one standard deviation away from the given mean? Another question is asking for two standard deviation, but if I understand how to get the first standard, I believe I can handle that problem. I prefer using DPYLR.

Upvotes: 0

Views: 181

Answers (1)

cdalitz
cdalitz

Reputation: 1277

A simple apporach is to utilize the following two R features:

  • comparison operators are vectorized, i.e. c(1,2) > c(2,0) yields c(FALSE,TRUE)
  • TRUE and FALSE are interpreted as 1 and 0 in arithemtic expressions

Thus, if x is the vector of your data points, you can count the number of datapoints in $\mu \pm \sigma$:

mu <- mean(x)
s <- sd(x)
sum( (x > mu - s) & (x < mu + s))

Upvotes: 1

Related Questions