user35131
user35131

Reputation: 1134

How do i find the standard deviation score for all values in R?

For example i have a mean based on function mean(value)= 1.278394 and i have a standard deviation for the values at .8028161

I would basically like the dataset to look like this

  value| sd score
1.278394       0
2.0812101      1
.4755779      -1
2.8840262      2
1.67980205   0.5

I guess you can call this sd score?

Upvotes: 0

Views: 381

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145745

"z score" is the common term for the number of standard deviations from the mean. You can calculate this easily for a vector, either manually or with the scale function:

my_mean = 1.278394 
my_sd = .8028161
x = rnorm(10, my_mean, my_sd)
z_score = (x - my_mean) / my_sd

# the scale function does this for you
scale(x, my_mean, my_sd) 

If you don't supply the mean and sd, scale will calculate it based on the x values. So in your data frame you can do something like:

my_data$z_score = scale(my_data$value)

and the scale defaults will do what you want.

Upvotes: 3

Related Questions