This function will compute z-scores with respect to the grand mean.

z_scored_grand_mean(data, cols, keep_original = TRUE)

Arguments

data

A data.frame or a data.frame extension (e.g. a tibble).

cols

Columns that need to be centered. See `dplyr::dplyr_tidy_select` for available options.

keep_original

default is `FALSE`. Set to `TRUE` to keep original columns

Value

An object of the same type as .data. The output has the following properties: 1. Columns from .data will be preserved 2. Columns with scores that are z-scored

Examples

z_scored_grand_mean(iris,where(is.numeric))
#>     Sepal.Length_z Sepal.Width_z Petal.Length_z Petal.Width_z    Species
#> 1      -0.89767388    1.01560199    -1.33575163 -1.3110521482     setosa
#> 2      -1.13920048   -0.13153881    -1.33575163 -1.3110521482     setosa
#> 3      -1.38072709    0.32731751    -1.39239929 -1.3110521482     setosa
#> 4      -1.50149039    0.09788935    -1.27910398 -1.3110521482     setosa
#> 5      -1.01843718    1.24503015    -1.33575163 -1.3110521482     setosa
#> 6      -0.53538397    1.93331463    -1.16580868 -1.0486667950     setosa
#> 7      -1.50149039    0.78617383    -1.33575163 -1.1798594716     setosa
#> 8      -1.01843718    0.78617383    -1.27910398 -1.3110521482     setosa
#> 9      -1.74301699   -0.36096697    -1.33575163 -1.3110521482     setosa
#> 10     -1.13920048    0.09788935    -1.27910398 -1.4422448248     setosa
#> 11     -0.53538397    1.47445831    -1.27910398 -1.3110521482     setosa
#> 12     -1.25996379    0.78617383    -1.22245633 -1.3110521482     setosa
#> 13     -1.25996379   -0.13153881    -1.33575163 -1.4422448248     setosa
#> 14     -1.86378030   -0.13153881    -1.50569459 -1.4422448248     setosa
#> 15     -0.05233076    2.16274279    -1.44904694 -1.3110521482     setosa
#> 16     -0.17309407    3.08045544    -1.27910398 -1.0486667950     setosa
#> 17     -0.53538397    1.93331463    -1.39239929 -1.0486667950     setosa
#> 18     -0.89767388    1.01560199    -1.33575163 -1.1798594716     setosa
#> 19     -0.17309407    1.70388647    -1.16580868 -1.1798594716     setosa
#> 20     -0.89767388    1.70388647    -1.27910398 -1.1798594716     setosa
#> 21     -0.53538397    0.78617383    -1.16580868 -1.3110521482     setosa
#> 22     -0.89767388    1.47445831    -1.27910398 -1.0486667950     setosa
#> 23     -1.50149039    1.24503015    -1.56234224 -1.3110521482     setosa
#> 24     -0.89767388    0.55674567    -1.16580868 -0.9174741184     setosa
#> 25     -1.25996379    0.78617383    -1.05251337 -1.3110521482     setosa
#> 26     -1.01843718   -0.13153881    -1.22245633 -1.3110521482     setosa
#> 27     -1.01843718    0.78617383    -1.22245633 -1.0486667950     setosa
#> 28     -0.77691058    1.01560199    -1.27910398 -1.3110521482     setosa
#> 29     -0.77691058    0.78617383    -1.33575163 -1.3110521482     setosa
#> 30     -1.38072709    0.32731751    -1.22245633 -1.3110521482     setosa
#> 31     -1.25996379    0.09788935    -1.22245633 -1.3110521482     setosa
#> 32     -0.53538397    0.78617383    -1.27910398 -1.0486667950     setosa
#> 33     -0.77691058    2.39217095    -1.27910398 -1.4422448248     setosa
#> 34     -0.41462067    2.62159911    -1.33575163 -1.3110521482     setosa
#> 35     -1.13920048    0.09788935    -1.27910398 -1.3110521482     setosa
#> 36     -1.01843718    0.32731751    -1.44904694 -1.3110521482     setosa
#> 37     -0.41462067    1.01560199    -1.39239929 -1.3110521482     setosa
#> 38     -1.13920048    1.24503015    -1.33575163 -1.4422448248     setosa
#> 39     -1.74301699   -0.13153881    -1.39239929 -1.3110521482     setosa
#> 40     -0.89767388    0.78617383    -1.27910398 -1.3110521482     setosa
#> 41     -1.01843718    1.01560199    -1.39239929 -1.1798594716     setosa
#> 42     -1.62225369   -1.73753594    -1.39239929 -1.1798594716     setosa
#> 43     -1.74301699    0.32731751    -1.39239929 -1.3110521482     setosa
#> 44     -1.01843718    1.01560199    -1.22245633 -0.7862814418     setosa
#> 45     -0.89767388    1.70388647    -1.05251337 -1.0486667950     setosa
#> 46     -1.25996379   -0.13153881    -1.33575163 -1.1798594716     setosa
#> 47     -0.89767388    1.70388647    -1.22245633 -1.3110521482     setosa
#> 48     -1.50149039    0.32731751    -1.33575163 -1.3110521482     setosa
#> 49     -0.65614727    1.47445831    -1.27910398 -1.3110521482     setosa
#> 50     -1.01843718    0.55674567    -1.33575163 -1.3110521482     setosa
#> 51      1.39682886    0.32731751     0.53362088  0.2632599711 versicolor
#> 52      0.67224905    0.32731751     0.42032558  0.3944526477 versicolor
#> 53      1.27606556    0.09788935     0.64691619  0.3944526477 versicolor
#> 54     -0.41462067   -1.73753594     0.13708732  0.1320672944 versicolor
#> 55      0.79301235   -0.59039513     0.47697323  0.3944526477 versicolor
#> 56     -0.17309407   -0.59039513     0.42032558  0.1320672944 versicolor
#> 57      0.55148575    0.55674567     0.53362088  0.5256453243 versicolor
#> 58     -1.13920048   -1.50810778    -0.25944625 -0.2615107354 versicolor
#> 59      0.91377565   -0.36096697     0.47697323  0.1320672944 versicolor
#> 60     -0.77691058   -0.81982329     0.08043967  0.2632599711 versicolor
#> 61     -1.01843718   -2.42582042    -0.14615094 -0.2615107354 versicolor
#> 62      0.06843254   -0.13153881     0.25038262  0.3944526477 versicolor
#> 63      0.18919584   -1.96696410     0.13708732 -0.2615107354 versicolor
#> 64      0.30995914   -0.36096697     0.53362088  0.2632599711 versicolor
#> 65     -0.29385737   -0.36096697    -0.08950329  0.1320672944 versicolor
#> 66      1.03453895    0.09788935     0.36367793  0.2632599711 versicolor
#> 67     -0.29385737   -0.13153881     0.42032558  0.3944526477 versicolor
#> 68     -0.05233076   -0.81982329     0.19373497 -0.2615107354 versicolor
#> 69      0.43072244   -1.96696410     0.42032558  0.3944526477 versicolor
#> 70     -0.29385737   -1.27867961     0.08043967 -0.1303180588 versicolor
#> 71      0.06843254    0.32731751     0.59026853  0.7880306775 versicolor
#> 72      0.30995914   -0.59039513     0.13708732  0.1320672944 versicolor
#> 73      0.55148575   -1.27867961     0.64691619  0.3944526477 versicolor
#> 74      0.30995914   -0.59039513     0.53362088  0.0008746178 versicolor
#> 75      0.67224905   -0.36096697     0.30703027  0.1320672944 versicolor
#> 76      0.91377565   -0.13153881     0.36367793  0.2632599711 versicolor
#> 77      1.15530226   -0.59039513     0.59026853  0.2632599711 versicolor
#> 78      1.03453895   -0.13153881     0.70356384  0.6568380009 versicolor
#> 79      0.18919584   -0.36096697     0.42032558  0.3944526477 versicolor
#> 80     -0.17309407   -1.04925145    -0.14615094 -0.2615107354 versicolor
#> 81     -0.41462067   -1.50810778     0.02379201 -0.1303180588 versicolor
#> 82     -0.41462067   -1.50810778    -0.03285564 -0.2615107354 versicolor
#> 83     -0.05233076   -0.81982329     0.08043967  0.0008746178 versicolor
#> 84      0.18919584   -0.81982329     0.76021149  0.5256453243 versicolor
#> 85     -0.53538397   -0.13153881     0.42032558  0.3944526477 versicolor
#> 86      0.18919584    0.78617383     0.42032558  0.5256453243 versicolor
#> 87      1.03453895    0.09788935     0.53362088  0.3944526477 versicolor
#> 88      0.55148575   -1.73753594     0.36367793  0.1320672944 versicolor
#> 89     -0.29385737   -0.13153881     0.19373497  0.1320672944 versicolor
#> 90     -0.41462067   -1.27867961     0.13708732  0.1320672944 versicolor
#> 91     -0.41462067   -1.04925145     0.36367793  0.0008746178 versicolor
#> 92      0.30995914   -0.13153881     0.47697323  0.2632599711 versicolor
#> 93     -0.05233076   -1.04925145     0.13708732  0.0008746178 versicolor
#> 94     -1.01843718   -1.73753594    -0.25944625 -0.2615107354 versicolor
#> 95     -0.29385737   -0.81982329     0.25038262  0.1320672944 versicolor
#> 96     -0.17309407   -0.13153881     0.25038262  0.0008746178 versicolor
#> 97     -0.17309407   -0.36096697     0.25038262  0.1320672944 versicolor
#> 98      0.43072244   -0.36096697     0.30703027  0.1320672944 versicolor
#> 99     -0.89767388   -1.27867961    -0.42938920 -0.1303180588 versicolor
#> 100    -0.17309407   -0.59039513     0.19373497  0.1320672944 versicolor
#> 101     0.55148575    0.55674567     1.27004036  1.7063794137  virginica
#> 102    -0.05233076   -0.81982329     0.76021149  0.9192233541  virginica
#> 103     1.51759216   -0.13153881     1.21339271  1.1816087073  virginica
#> 104     0.55148575   -0.36096697     1.04344975  0.7880306775  virginica
#> 105     0.79301235   -0.13153881     1.15674505  1.3128013839  virginica
#> 106     2.12140867   -0.13153881     1.60992627  1.1816087073  virginica
#> 107    -1.13920048   -1.27867961     0.42032558  0.6568380009  virginica
#> 108     1.75911877   -0.36096697     1.43998331  0.7880306775  virginica
#> 109     1.03453895   -1.27867961     1.15674505  0.7880306775  virginica
#> 110     1.63835547    1.24503015     1.32668801  1.7063794137  virginica
#> 111     0.79301235    0.32731751     0.76021149  1.0504160307  virginica
#> 112     0.67224905   -0.81982329     0.87350679  0.9192233541  virginica
#> 113     1.15530226   -0.13153881     0.98680210  1.1816087073  virginica
#> 114    -0.17309407   -1.27867961     0.70356384  1.0504160307  virginica
#> 115    -0.05233076   -0.59039513     0.76021149  1.5751867371  virginica
#> 116     0.67224905    0.32731751     0.87350679  1.4439940605  virginica
#> 117     0.79301235   -0.13153881     0.98680210  0.7880306775  virginica
#> 118     2.24217198    1.70388647     1.66657392  1.3128013839  virginica
#> 119     2.24217198   -1.04925145     1.77986923  1.4439940605  virginica
#> 120     0.18919584   -1.96696410     0.70356384  0.3944526477  virginica
#> 121     1.27606556    0.32731751     1.10009740  1.4439940605  virginica
#> 122    -0.29385737   -0.59039513     0.64691619  1.0504160307  virginica
#> 123     2.24217198   -0.59039513     1.66657392  1.0504160307  virginica
#> 124     0.55148575   -0.81982329     0.64691619  0.7880306775  virginica
#> 125     1.03453895    0.55674567     1.10009740  1.1816087073  virginica
#> 126     1.63835547    0.32731751     1.27004036  0.7880306775  virginica
#> 127     0.43072244   -0.59039513     0.59026853  0.7880306775  virginica
#> 128     0.30995914   -0.13153881     0.64691619  0.7880306775  virginica
#> 129     0.67224905   -0.59039513     1.04344975  1.1816087073  virginica
#> 130     1.63835547   -0.13153881     1.15674505  0.5256453243  virginica
#> 131     1.87988207   -0.59039513     1.32668801  0.9192233541  virginica
#> 132     2.48369858    1.70388647     1.49663097  1.0504160307  virginica
#> 133     0.67224905   -0.59039513     1.04344975  1.3128013839  virginica
#> 134     0.55148575   -0.59039513     0.76021149  0.3944526477  virginica
#> 135     0.30995914   -1.04925145     1.04344975  0.2632599711  virginica
#> 136     2.24217198   -0.13153881     1.32668801  1.4439940605  virginica
#> 137     0.55148575    0.78617383     1.04344975  1.5751867371  virginica
#> 138     0.67224905    0.09788935     0.98680210  0.7880306775  virginica
#> 139     0.18919584   -0.13153881     0.59026853  0.7880306775  virginica
#> 140     1.27606556    0.09788935     0.93015445  1.1816087073  virginica
#> 141     1.03453895    0.09788935     1.04344975  1.5751867371  virginica
#> 142     1.27606556    0.09788935     0.76021149  1.4439940605  virginica
#> 143    -0.05233076   -0.81982329     0.76021149  0.9192233541  virginica
#> 144     1.15530226    0.32731751     1.21339271  1.4439940605  virginica
#> 145     1.03453895    0.55674567     1.10009740  1.7063794137  virginica
#> 146     1.03453895   -0.13153881     0.81685914  1.4439940605  virginica
#> 147     0.55148575   -1.27867961     0.70356384  0.9192233541  virginica
#> 148     0.79301235   -0.13153881     0.81685914  1.0504160307  virginica
#> 149     0.43072244    0.78617383     0.93015445  1.4439940605  virginica
#> 150     0.06843254   -0.13153881     0.76021149  0.7880306775  virginica
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1            5.1         3.5          1.4         0.2
#> 2            4.9         3.0          1.4         0.2
#> 3            4.7         3.2          1.3         0.2
#> 4            4.6         3.1          1.5         0.2
#> 5            5.0         3.6          1.4         0.2
#> 6            5.4         3.9          1.7         0.4
#> 7            4.6         3.4          1.4         0.3
#> 8            5.0         3.4          1.5         0.2
#> 9            4.4         2.9          1.4         0.2
#> 10           4.9         3.1          1.5         0.1
#> 11           5.4         3.7          1.5         0.2
#> 12           4.8         3.4          1.6         0.2
#> 13           4.8         3.0          1.4         0.1
#> 14           4.3         3.0          1.1         0.1
#> 15           5.8         4.0          1.2         0.2
#> 16           5.7         4.4          1.5         0.4
#> 17           5.4         3.9          1.3         0.4
#> 18           5.1         3.5          1.4         0.3
#> 19           5.7         3.8          1.7         0.3
#> 20           5.1         3.8          1.5         0.3
#> 21           5.4         3.4          1.7         0.2
#> 22           5.1         3.7          1.5         0.4
#> 23           4.6         3.6          1.0         0.2
#> 24           5.1         3.3          1.7         0.5
#> 25           4.8         3.4          1.9         0.2
#> 26           5.0         3.0          1.6         0.2
#> 27           5.0         3.4          1.6         0.4
#> 28           5.2         3.5          1.5         0.2
#> 29           5.2         3.4          1.4         0.2
#> 30           4.7         3.2          1.6         0.2
#> 31           4.8         3.1          1.6         0.2
#> 32           5.4         3.4          1.5         0.4
#> 33           5.2         4.1          1.5         0.1
#> 34           5.5         4.2          1.4         0.2
#> 35           4.9         3.1          1.5         0.2
#> 36           5.0         3.2          1.2         0.2
#> 37           5.5         3.5          1.3         0.2
#> 38           4.9         3.6          1.4         0.1
#> 39           4.4         3.0          1.3         0.2
#> 40           5.1         3.4          1.5         0.2
#> 41           5.0         3.5          1.3         0.3
#> 42           4.5         2.3          1.3         0.3
#> 43           4.4         3.2          1.3         0.2
#> 44           5.0         3.5          1.6         0.6
#> 45           5.1         3.8          1.9         0.4
#> 46           4.8         3.0          1.4         0.3
#> 47           5.1         3.8          1.6         0.2
#> 48           4.6         3.2          1.4         0.2
#> 49           5.3         3.7          1.5         0.2
#> 50           5.0         3.3          1.4         0.2
#> 51           7.0         3.2          4.7         1.4
#> 52           6.4         3.2          4.5         1.5
#> 53           6.9         3.1          4.9         1.5
#> 54           5.5         2.3          4.0         1.3
#> 55           6.5         2.8          4.6         1.5
#> 56           5.7         2.8          4.5         1.3
#> 57           6.3         3.3          4.7         1.6
#> 58           4.9         2.4          3.3         1.0
#> 59           6.6         2.9          4.6         1.3
#> 60           5.2         2.7          3.9         1.4
#> 61           5.0         2.0          3.5         1.0
#> 62           5.9         3.0          4.2         1.5
#> 63           6.0         2.2          4.0         1.0
#> 64           6.1         2.9          4.7         1.4
#> 65           5.6         2.9          3.6         1.3
#> 66           6.7         3.1          4.4         1.4
#> 67           5.6         3.0          4.5         1.5
#> 68           5.8         2.7          4.1         1.0
#> 69           6.2         2.2          4.5         1.5
#> 70           5.6         2.5          3.9         1.1
#> 71           5.9         3.2          4.8         1.8
#> 72           6.1         2.8          4.0         1.3
#> 73           6.3         2.5          4.9         1.5
#> 74           6.1         2.8          4.7         1.2
#> 75           6.4         2.9          4.3         1.3
#> 76           6.6         3.0          4.4         1.4
#> 77           6.8         2.8          4.8         1.4
#> 78           6.7         3.0          5.0         1.7
#> 79           6.0         2.9          4.5         1.5
#> 80           5.7         2.6          3.5         1.0
#> 81           5.5         2.4          3.8         1.1
#> 82           5.5         2.4          3.7         1.0
#> 83           5.8         2.7          3.9         1.2
#> 84           6.0         2.7          5.1         1.6
#> 85           5.4         3.0          4.5         1.5
#> 86           6.0         3.4          4.5         1.6
#> 87           6.7         3.1          4.7         1.5
#> 88           6.3         2.3          4.4         1.3
#> 89           5.6         3.0          4.1         1.3
#> 90           5.5         2.5          4.0         1.3
#> 91           5.5         2.6          4.4         1.2
#> 92           6.1         3.0          4.6         1.4
#> 93           5.8         2.6          4.0         1.2
#> 94           5.0         2.3          3.3         1.0
#> 95           5.6         2.7          4.2         1.3
#> 96           5.7         3.0          4.2         1.2
#> 97           5.7         2.9          4.2         1.3
#> 98           6.2         2.9          4.3         1.3
#> 99           5.1         2.5          3.0         1.1
#> 100          5.7         2.8          4.1         1.3
#> 101          6.3         3.3          6.0         2.5
#> 102          5.8         2.7          5.1         1.9
#> 103          7.1         3.0          5.9         2.1
#> 104          6.3         2.9          5.6         1.8
#> 105          6.5         3.0          5.8         2.2
#> 106          7.6         3.0          6.6         2.1
#> 107          4.9         2.5          4.5         1.7
#> 108          7.3         2.9          6.3         1.8
#> 109          6.7         2.5          5.8         1.8
#> 110          7.2         3.6          6.1         2.5
#> 111          6.5         3.2          5.1         2.0
#> 112          6.4         2.7          5.3         1.9
#> 113          6.8         3.0          5.5         2.1
#> 114          5.7         2.5          5.0         2.0
#> 115          5.8         2.8          5.1         2.4
#> 116          6.4         3.2          5.3         2.3
#> 117          6.5         3.0          5.5         1.8
#> 118          7.7         3.8          6.7         2.2
#> 119          7.7         2.6          6.9         2.3
#> 120          6.0         2.2          5.0         1.5
#> 121          6.9         3.2          5.7         2.3
#> 122          5.6         2.8          4.9         2.0
#> 123          7.7         2.8          6.7         2.0
#> 124          6.3         2.7          4.9         1.8
#> 125          6.7         3.3          5.7         2.1
#> 126          7.2         3.2          6.0         1.8
#> 127          6.2         2.8          4.8         1.8
#> 128          6.1         3.0          4.9         1.8
#> 129          6.4         2.8          5.6         2.1
#> 130          7.2         3.0          5.8         1.6
#> 131          7.4         2.8          6.1         1.9
#> 132          7.9         3.8          6.4         2.0
#> 133          6.4         2.8          5.6         2.2
#> 134          6.3         2.8          5.1         1.5
#> 135          6.1         2.6          5.6         1.4
#> 136          7.7         3.0          6.1         2.3
#> 137          6.3         3.4          5.6         2.4
#> 138          6.4         3.1          5.5         1.8
#> 139          6.0         3.0          4.8         1.8
#> 140          6.9         3.1          5.4         2.1
#> 141          6.7         3.1          5.6         2.4
#> 142          6.9         3.1          5.1         2.3
#> 143          5.8         2.7          5.1         1.9
#> 144          6.8         3.2          5.9         2.3
#> 145          6.7         3.3          5.7         2.5
#> 146          6.7         3.0          5.2         2.3
#> 147          6.3         2.5          5.0         1.9
#> 148          6.5         3.0          5.2         2.0
#> 149          6.2         3.4          5.4         2.3
#> 150          5.9         3.0          5.1         1.8