I would really appreciate your help in this question. I have the following dataset and I would like to create a new variable which would contain the standardized values (z distribution) per level of a given factor variable.
x <- data.frame(gender = c("boy","boy","boy","girl","girl","girl"),
values=c(1,2,3,6,7,8))
x
gender values
1 boy 1
2 boy 2
3 boy 3
4 girl 6
5 girl 7
6 girl 8
My aim is to create one new variable which will contain the z-values calculated separately for each factor level (for boys and for girls).
And another question. I mainly would like to create a variable with the z-values. Would it be similar if I would like to apply another function and for example calculate distribution in quantiles per factor level?
Thank you for your help!
You can use
scale
withave
andtransform
:aggregate
is also usefulAnd there are a lot of ways using
ddply
from plyr,tapply
,data.table
. Take a look at this post