How to calculate the z score for grouped data in R?

R ProgrammingServer Side ProgrammingProgramming

To calculate the z score for grouped data, we can use ave function and scale function. For example, if we have a data frame called df that contains a grouping coloumn say GROUP and a numerical column say Response then we can use the below command to calculate the z score for this data −

ave(df$Response,df$GROUP,FUN=scale)

Example

Consider the below data frame −

 Live Demo

grp<-sample(LETTERS[1:3],20,replace=TRUE)
Response<-rpois(20,8)
df1<-data.frame(grp,Response)
df1

Output

 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

Calculating z score for Response by grouping column grp −

Example

ave(df1$Response,df1$grp,FUN=scale)

Output

[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

Example

 Live Demo

class<-sample(c("first","second","third"),20,replace=TRUE)
Y<-rnorm(20)
df2<-data.frame(class,Y)
df2

Output

    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

Calculating z score for Y by grouping column class −

Example

ave(df2$Y,df2$class,FUN=scale)

Output

[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
raja
Published on 16-Mar-2021 10:55:08
Advertisements