Skip to contents

Centers a vector, a matrix or a data frame to the grand mean or the group mean.

Usage

center(X, group = NULL, grandmean = NULL, groupmean = NULL, wt = NULL)

grand.mean(x, wt = NULL)

group.mean(x, group, wt = NULL)

getgroup.mean(x, group, wt = NULL)

Arguments

X

a matrix or a data frame.

group

a vector indicating the group for centering.

grandmean

a numeric or character vector indicating the number or the the names of columns of X to which grand-mean should be applied.

groupmean

a numeric or character vector indicating the number or the the names of columns of X to which group-mean should be applied.

wt

a numeric vector of weights.

x

a vector, a matrix or a data frame.

Value

a data frame, or a vector.

Examples

# Less data for shorter example
repdata2 <- repdata[1:10,c(1:3,6:10,51)]

### One variable ----

# grand-mean
grand.mean(repdata2$item01)
#>  [1]  0.25 -0.75    NA  0.25  0.25 -0.75    NA  0.25  0.25  0.25
grand.mean(repdata2$item01,wt = repdata2$wt)
#>  [1]  0.2352684 -0.7647316         NA  0.2352684  0.2352684 -0.7647316
#>  [7]         NA  0.2352684  0.2352684  0.2352684

# group-mean
group.mean(repdata2$item01,group = repdata2$GROUP)
#>  [1]  0.50 -0.75    NA  0.25  0.00 -0.50    NA  0.25  0.00  0.25
group.mean(repdata2$item01,group = repdata2$GROUP,wt = repdata2$wt)
#>  [1]  0.5422923 -0.8069408         NA  0.1930592  0.0000000 -0.4577077
#>  [7]         NA  0.1930592  0.0000000  0.1930592

### More than one variable with the same rule ----

# grand-mean
grand.mean(repdata2[,4:8])
#>    item01 item02     item03     item04 item05
#> 1    0.25   0.75         NA  0.1111111     NA
#> 2   -0.75  -1.25  0.4444444  0.1111111  -1.25
#> 3      NA  -0.25  0.4444444  0.1111111  -0.25
#> 4    0.25   0.75 -2.5555556         NA   0.75
#> 5    0.25     NA  0.4444444  0.1111111  -0.25
#> 6   -0.75     NA  0.4444444  0.1111111     NA
#> 7      NA  -1.25 -0.5555556 -0.8888889  -1.25
#> 8    0.25  -0.25  0.4444444  0.1111111  -0.25
#> 9    0.25   0.75  0.4444444  0.1111111   1.75
#> 10   0.25   0.75  0.4444444  0.1111111   0.75
grand.mean(repdata2[,4:8],wt = repdata2$wt)
#>        item01     item02     item03     item04    item05
#> 1   0.2352684  0.7258867         NA  0.1248392        NA
#> 2  -0.7647316 -1.2741133  0.3846446  0.1248392 -1.307588
#> 3          NA -0.2741133  0.3846446  0.1248392 -0.307588
#> 4   0.2352684  0.7258867 -2.6153554         NA  0.692412
#> 5   0.2352684         NA  0.3846446  0.1248392 -0.307588
#> 6  -0.7647316         NA  0.3846446  0.1248392        NA
#> 7          NA -1.2741133 -0.6153554 -0.8751608 -1.307588
#> 8   0.2352684 -0.2741133  0.3846446  0.1248392 -0.307588
#> 9   0.2352684  0.7258867  0.3846446  0.1248392  1.692412
#> 10  0.2352684  0.7258867  0.3846446  0.1248392  0.692412

# group-mean
group.mean(repdata2[,4:8],group = repdata2$GROUP)
#>    item01 item02     item03 item04 item05
#> 1    0.50   1.00         NA   0.25     NA
#> 2   -0.75  -1.25  0.7500000   0.00  -1.25
#> 3      NA   0.00  0.3333333   0.25   0.50
#> 4    0.25   0.75 -2.2500000     NA   0.75
#> 5    0.00     NA  0.0000000   0.00  -1.00
#> 6   -0.50     NA  0.3333333   0.25     NA
#> 7      NA  -1.00 -0.6666667  -0.75  -0.50
#> 8    0.25  -0.25  0.7500000   0.00  -0.25
#> 9    0.00   0.00  0.0000000   0.00   1.00
#> 10   0.25   0.75  0.7500000   0.00   0.75
group.mean(repdata2[,4:8],group = repdata2$GROUP,wt = repdata2$wt)
#>        item01      item02     item03     item04     item05
#> 1   0.5422923  1.06802387         NA  0.2816808         NA
#> 2  -0.8069408 -1.31405631  0.5966861  0.0000000 -1.3140563
#> 3          NA  0.06802387  0.3669564  0.2816808  0.5721930
#> 4   0.1930592  0.68594369 -2.4033139         NA  0.6859437
#> 5   0.0000000          NA  0.0000000  0.0000000 -1.1767053
#> 6  -0.4577077          NA  0.3669564  0.2816808         NA
#> 7          NA -0.93197613 -0.6330436 -0.7183192 -0.4278070
#> 8   0.1930592 -0.31405631  0.5966861  0.0000000 -0.3140563
#> 9   0.0000000  0.00000000  0.0000000  0.0000000  0.8232947
#> 10  0.1930592  0.68594369  0.5966861  0.0000000  0.6859437

### More than one variable with different rules ----
center(repdata2, group = repdata2$GROUP, grandmean = 4:5, groupmean = 6:8, wt = repdata2$wt)
#>    GROUP ID GENDER     item01     item02     item03     item04     item05
#> 1    GR1  1      0  0.2352684  0.7258867         NA  0.2816808         NA
#> 2    GR3  2      0 -0.7647316 -1.2741133  0.5966861  0.0000000 -1.3140563
#> 3    GR1  3      0         NA -0.2741133  0.3669564  0.2816808  0.5721930
#> 4    GR3  4      1  0.2352684  0.7258867 -2.4033139         NA  0.6859437
#> 5    GR2  5      1  0.2352684         NA  0.0000000  0.0000000 -1.1767053
#> 6    GR1  6      0 -0.7647316         NA  0.3669564  0.2816808         NA
#> 7    GR1  7      1         NA -1.2741133 -0.6330436 -0.7183192 -0.4278070
#> 8    GR3  8      0  0.2352684 -0.2741133  0.5966861  0.0000000 -0.3140563
#> 9    GR2  9      1  0.2352684  0.7258867  0.0000000  0.0000000  0.8232947
#> 10   GR3 10      0  0.2352684  0.7258867  0.5966861  0.0000000  0.6859437
#>          wt
#> 1  1.491828
#> 2  1.183717
#> 3  1.351983
#> 4  1.219501
#> 5  1.298092
#> 6  1.767518
#> 7  1.808281
#> 8  1.838340
#> 9  1.855316
#> 10 1.889812
center(repdata2, group = repdata2$GROUP, grandmean = 6:8, groupmean = 4:5, wt = repdata2$wt)
#>    GROUP ID GENDER     item01      item02     item03     item04    item05
#> 1    GR1  1      0  0.5422923  1.06802387         NA  0.1248392        NA
#> 2    GR3  2      0 -0.8069408 -1.31405631  0.3846446  0.1248392 -1.307588
#> 3    GR1  3      0         NA  0.06802387  0.3846446  0.1248392 -0.307588
#> 4    GR3  4      1  0.1930592  0.68594369 -2.6153554         NA  0.692412
#> 5    GR2  5      1  0.0000000          NA  0.3846446  0.1248392 -0.307588
#> 6    GR1  6      0 -0.4577077          NA  0.3846446  0.1248392        NA
#> 7    GR1  7      1         NA -0.93197613 -0.6153554 -0.8751608 -1.307588
#> 8    GR3  8      0  0.1930592 -0.31405631  0.3846446  0.1248392 -0.307588
#> 9    GR2  9      1  0.0000000  0.00000000  0.3846446  0.1248392  1.692412
#> 10   GR3 10      0  0.1930592  0.68594369  0.3846446  0.1248392  0.692412
#>          wt
#> 1  1.491828
#> 2  1.183717
#> 3  1.351983
#> 4  1.219501
#> 5  1.298092
#> 6  1.767518
#> 7  1.808281
#> 8  1.838340
#> 9  1.855316
#> 10 1.889812

center(repdata2, group = repdata2$GROUP, wt = repdata2$wt,
       grandmean = paste0("item0",1:3), groupmean = paste0("item0",4:5))
#>    GROUP ID GENDER     item01     item02     item03     item04     item05
#> 1    GR1  1      0  0.2352684  0.7258867         NA  0.2816808         NA
#> 2    GR3  2      0 -0.7647316 -1.2741133  0.3846446  0.0000000 -1.3140563
#> 3    GR1  3      0         NA -0.2741133  0.3846446  0.2816808  0.5721930
#> 4    GR3  4      1  0.2352684  0.7258867 -2.6153554         NA  0.6859437
#> 5    GR2  5      1  0.2352684         NA  0.3846446  0.0000000 -1.1767053
#> 6    GR1  6      0 -0.7647316         NA  0.3846446  0.2816808         NA
#> 7    GR1  7      1         NA -1.2741133 -0.6153554 -0.7183192 -0.4278070
#> 8    GR3  8      0  0.2352684 -0.2741133  0.3846446  0.0000000 -0.3140563
#> 9    GR2  9      1  0.2352684  0.7258867  0.3846446  0.0000000  0.8232947
#> 10   GR3 10      0  0.2352684  0.7258867  0.3846446  0.0000000  0.6859437
#>          wt
#> 1  1.491828
#> 2  1.183717
#> 3  1.351983
#> 4  1.219501
#> 5  1.298092
#> 6  1.767518
#> 7  1.808281
#> 8  1.838340
#> 9  1.855316
#> 10 1.889812
center(repdata2, group = repdata2$GROUP, wt = repdata2$wt,
       grandmean = paste0("item0",4:5), groupmean = paste0("item0",1:3))
#>    GROUP ID GENDER     item01      item02     item03     item04    item05
#> 1    GR1  1      0  0.5422923  1.06802387         NA  0.1248392        NA
#> 2    GR3  2      0 -0.8069408 -1.31405631  0.5966861  0.1248392 -1.307588
#> 3    GR1  3      0         NA  0.06802387  0.3669564  0.1248392 -0.307588
#> 4    GR3  4      1  0.1930592  0.68594369 -2.4033139         NA  0.692412
#> 5    GR2  5      1  0.0000000          NA  0.0000000  0.1248392 -0.307588
#> 6    GR1  6      0 -0.4577077          NA  0.3669564  0.1248392        NA
#> 7    GR1  7      1         NA -0.93197613 -0.6330436 -0.8751608 -1.307588
#> 8    GR3  8      0  0.1930592 -0.31405631  0.5966861  0.1248392 -0.307588
#> 9    GR2  9      1  0.0000000  0.00000000  0.0000000  0.1248392  1.692412
#> 10   GR3 10      0  0.1930592  0.68594369  0.5966861  0.1248392  0.692412
#>          wt
#> 1  1.491828
#> 2  1.183717
#> 3  1.351983
#> 4  1.219501
#> 5  1.298092
#> 6  1.767518
#> 7  1.808281
#> 8  1.838340
#> 9  1.855316
#> 10 1.889812