如何计算R中分组数据的z分数?
要计算分组数据的z分数,我们可以使用ave函数和scale函数。例如,如果我们有一个名为df的数据框,其中包含一个分组列(如GROUP)和一个数字列(如Response),那么我们可以使用以下命令来计算该数据的z分数-
ave(df$Response,df$GROUP,FUN=scale)
示例
考虑以下数据框-
grp<-sample(LETTERS[1:3],20,replace=TRUE) Response<-rpois(20,8) df1<-data.frame(grp,Response) df1输出结果
grp Response 1 A 8 2 A 11 3 A 14 4 A 10 5 C 11 6 B 8 7 A 5 8 A 6 9 A 9 10 B 5 11 C 13 12 B 4 13 C 9 14 A 5 15 C 8 16 A 6 17 A 5 18 A 7 19 A 4 20 A 6
通过对列grp进行分组来计算响应的z分数-
示例
ave(df1$Response,df1$grp,FUN=scale)输出结果
[1] 0.2120368 1.2457162 2.2793957 0.9011564 0.3382407 1.1208971 [7] -0.8216426 -0.4770828 0.5565966 -0.3202563 1.2402159 -0.8006408 [13] -0.5637345 -0.8216426 -1.0147221 -0.4770828 -0.8216426 -0.1325230 [19] -1.1662024 -0.4770828
示例
class<-sample(c("first","second","third"),20,replace=TRUE) Y<-rnorm(20) df2<-data.frame(class,Y) df2输出结果
class Y 1 first -0.11728051 2 second 0.52111014 3 third 0.10489245 4 second -1.26830798 5 second 1.43719885 6 second -0.06000703 7 second 0.11140428 8 first -0.58586144 9 first 1.33474582 10 second 0.30187417 11 third 0.73870989 12 third -0.80603254 13 third 1.32022423 14 third -1.18979778 15 third 0.81707529 16 third -0.70428792 17 third 0.47795339 18 third 1.27301587 19 second 0.71368222 20 third -0.36603555
通过对列类进行分组来计算Y的z分数-
示例
ave(df2$Y,df2$class,FUN=scale)输出结果
[1] -0.32736040 0.32592308 -0.06879654 -1.83319129 1.43127698 -0.37525369 [7] -0.16842857 -0.79529132 1.12265172 0.06139266 0.63815798 -1.08483444 [13] 1.28677408 -1.51288290 0.72556607 -0.97134937 0.34731243 1.23411828 [19] 0.55828083 -0.59406558