Skip to contents

fmean is a generic function that computes the (column-wise) mean of x, (optionally) grouped by g and/or weighted by w. The TRA argument can further be used to transform x using its (grouped, weighted) mean.

Usage

fmean(x, ...)

# Default S3 method
fmean(x, g = NULL, w = NULL, TRA = NULL, na.rm = .op[["na.rm"]],
      use.g.names = TRUE, nthreads = .op[["nthreads"]], ...)

# S3 method for class 'matrix'
fmean(x, g = NULL, w = NULL, TRA = NULL, na.rm = .op[["na.rm"]],
      use.g.names = TRUE, drop = TRUE, nthreads = .op[["nthreads"]], ...)

# S3 method for class 'data.frame'
fmean(x, g = NULL, w = NULL, TRA = NULL, na.rm = .op[["na.rm"]],
      use.g.names = TRUE, drop = TRUE, nthreads = .op[["nthreads"]], ...)

# S3 method for class 'grouped_df'
fmean(x, w = NULL, TRA = NULL, na.rm = .op[["na.rm"]],
      use.g.names = FALSE, keep.group_vars = TRUE,
      keep.w = TRUE, stub = .op[["stub"]], nthreads = .op[["nthreads"]], ...)

Arguments

x

a numeric vector, matrix, data frame or grouped data frame (class 'grouped_df').

g

a factor, GRP object, atomic vector (internally converted to factor) or a list of vectors / factors (internally converted to a GRP object) used to group x.

w

a numeric vector of (non-negative) weights, may contain missing values.

TRA

an integer or quoted operator indicating the transformation to perform: 0 - "na" | 1 - "fill" | 2 - "replace" | 3 - "-" | 4 - "-+" | 5 - "/" | 6 - "%" | 7 - "+" | 8 - "*" | 9 - "%%" | 10 - "-%%". See TRA.

na.rm

logical. Skip missing values in x. Defaults to TRUE and implemented at very little computational cost. If na.rm = FALSE a NA is returned when encountered.

use.g.names

logical. Make group-names and add to the result as names (default method) or row-names (matrix and data frame methods). No row-names are generated for data.table's.

nthreads

integer. The number of threads to utilize. See Details of fsum.

drop

matrix and data.frame method: Logical. TRUE drops dimensions and returns an atomic vector if g = NULL and TRA = NULL.

keep.group_vars

grouped_df method: Logical. FALSE removes grouping variables after computation.

keep.w

grouped_df method: Logical. Retain summed weighting variable after computation (if contained in grouped_df).

stub

character. If keep.w = TRUE and stub = TRUE (default), the summed weights column is prefixed by "sum.". Users can specify a different prefix through this argument, or set it to FALSE to avoid prefixing.

...

arguments to be passed to or from other methods. If TRA is used, passing set = TRUE will transform data by reference and return the result invisibly.

Details

The weighted mean is computed as sum(x * w) / sum(w), using a single pass in C. If na.rm = TRUE, missing values will be removed from both x and w i.e. utilizing only x[complete.cases(x,w)] and w[complete.cases(x,w)].

For further computational details see fsum, which works equivalently.

Value

The (w weighted) mean of x, grouped by g, or (if TRA is used) x transformed by its (grouped, weighted) mean.

Examples

## default vector method
mpg <- mtcars$mpg
fmean(mpg)                         # Simple mean
#> [1] 20.09063
fmean(mpg, w = mtcars$hp)          # Weighted mean: Weighted by hp
#> [1] 17.97245
fmean(mpg, TRA = "-")              # Simple transformation: demeaning (See also ?W)
#>  [1]  0.909375  0.909375  2.709375  1.309375 -1.390625 -1.990625 -5.790625
#>  [8]  4.309375  2.709375 -0.890625 -2.290625 -3.690625 -2.790625 -4.890625
#> [15] -9.690625 -9.690625 -5.390625 12.309375 10.309375 13.809375  1.409375
#> [22] -4.590625 -4.890625 -6.790625 -0.890625  7.209375  5.909375 10.309375
#> [29] -4.290625 -0.390625 -5.090625  1.309375
fmean(mpg, mtcars$cyl)             # Grouped mean
#>        4        6        8 
#> 26.66364 19.74286 15.10000 
fmean(mpg, mtcars[8:9])            # another grouped mean.
#>      0.0      0.1      1.0      1.1 
#> 15.05000 19.75000 20.74286 28.37143 
g <- GRP(mtcars[c(2,8:9)])
fmean(mpg, g)                      # Pre-computing groups speeds up the computation
#>    4.0.1    4.1.0    4.1.1    6.0.1    6.1.0    8.0.0    8.0.1 
#> 26.00000 22.90000 28.37143 20.56667 19.12500 15.05000 15.40000 
fmean(mpg, g, mtcars$hp)           # Grouped weighted mean
#>    4.0.1    4.1.0    4.1.1    6.0.1    6.1.0    8.0.0    8.0.1 
#> 26.00000 22.69409 27.68209 20.42405 19.10087 14.82854 15.35259 
fmean(mpg, g, TRA = "-")           # Demeaning by group
#>  [1]  0.4333333  0.4333333 -5.5714286  2.2750000  3.6500000 -1.0250000
#>  [7] -0.7500000  1.5000000 -0.1000000  0.0750000 -1.3250000  1.3500000
#> [13]  2.2500000  0.1500000 -4.6500000 -4.6500000 -0.3500000  4.0285714
#> [19]  2.0285714  5.5285714 -1.4000000  0.4500000  0.1500000 -1.7500000
#> [25]  4.1500000 -1.0714286  0.0000000  2.0285714  0.4000000 -0.8666667
#> [31] -0.4000000 -6.9714286
fmean(mpg, g, mtcars$hp, "-")      # Group-demeaning using weighted group means
#>  [1]  0.57594937  0.57594937 -4.88209220  2.29913232  3.87145923 -1.00086768
#>  [7] -0.52854077  1.70590551  0.10590551  0.09913232 -1.30086768  1.57145923
#> [13]  2.47145923  0.37145923 -4.42854077 -4.42854077 -0.12854077  4.71790780
#> [19]  2.71790780  6.21790780 -1.19409449  0.67145923  0.37145923 -1.52854077
#> [25]  4.37145923 -0.38209220  0.00000000  2.71790780  0.44741235 -0.72405063
#> [31] -0.35258765 -6.28209220

## data.frame method
fmean(mtcars)
#>        mpg        cyl       disp         hp       drat         wt       qsec 
#>  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
#>         vs         am       gear       carb 
#>   0.437500   0.406250   3.687500   2.812500 
fmean(mtcars, g)
#>            mpg cyl     disp        hp     drat       wt     qsec vs am     gear
#> 4.0.1 26.00000   4 120.3000  91.00000 4.430000 2.140000 16.70000  0  1 5.000000
#> 4.1.0 22.90000   4 135.8667  84.66667 3.770000 2.935000 20.97000  1  0 3.666667
#> 4.1.1 28.37143   4  89.8000  80.57143 4.148571 2.028286 18.70000  1  1 4.142857
#> 6.0.1 20.56667   6 155.0000 131.66667 3.806667 2.755000 16.32667  0  1 4.333333
#> 6.1.0 19.12500   6 204.5500 115.25000 3.420000 3.388750 19.21500  1  0 3.500000
#> 8.0.0 15.05000   8 357.6167 194.16667 3.120833 4.104083 17.14250  0  0 3.000000
#> 8.0.1 15.40000   8 326.0000 299.50000 3.880000 3.370000 14.55000  0  1 5.000000
#>           carb
#> 4.0.1 2.000000
#> 4.1.0 1.666667
#> 4.1.1 1.428571
#> 6.0.1 4.666667
#> 6.1.0 2.500000
#> 8.0.0 3.083333
#> 8.0.1 6.000000
fmean(fgroup_by(mtcars, cyl, vs, am))  # Another way of doing it..
#>   cyl vs am      mpg     disp        hp     drat       wt     qsec     gear
#> 1   4  0  1 26.00000 120.3000  91.00000 4.430000 2.140000 16.70000 5.000000
#> 2   4  1  0 22.90000 135.8667  84.66667 3.770000 2.935000 20.97000 3.666667
#> 3   4  1  1 28.37143  89.8000  80.57143 4.148571 2.028286 18.70000 4.142857
#> 4   6  0  1 20.56667 155.0000 131.66667 3.806667 2.755000 16.32667 4.333333
#> 5   6  1  0 19.12500 204.5500 115.25000 3.420000 3.388750 19.21500 3.500000
#> 6   8  0  0 15.05000 357.6167 194.16667 3.120833 4.104083 17.14250 3.000000
#> 7   8  0  1 15.40000 326.0000 299.50000 3.880000 3.370000 14.55000 5.000000
#>       carb
#> 1 2.000000
#> 2 1.666667
#> 3 1.428571
#> 4 4.666667
#> 5 2.500000
#> 6 3.083333
#> 7 6.000000
head(fmean(mtcars, g, TRA = "-"))      # etc..
#>                          mpg cyl      disp        hp        drat         wt
#> Mazda RX4          0.4333333   0  5.000000 -21.66667  0.09333333 -0.1350000
#> Mazda RX4 Wag      0.4333333   0  5.000000 -21.66667  0.09333333  0.1200000
#> Datsun 710        -5.5714286   0 18.200000  12.42857 -0.29857143  0.2917143
#> Hornet 4 Drive     2.2750000   0 53.450000  -5.25000 -0.34000000 -0.1737500
#> Hornet Sportabout  3.6500000   0  2.383333 -19.16667  0.02916667 -0.6640833
#> Valiant           -1.0250000   0 20.450000 -10.25000 -0.66000000  0.0712500
#>                         qsec vs am       gear       carb
#> Mazda RX4          0.1333333  0  0 -0.3333333 -0.6666667
#> Mazda RX4 Wag      0.6933333  0  0 -0.3333333 -0.6666667
#> Datsun 710        -0.0900000  0  0 -0.1428571 -0.4285714
#> Hornet 4 Drive     0.2250000  0  0 -0.5000000 -1.5000000
#> Hornet Sportabout -0.1225000  0  0  0.0000000 -1.0833333
#> Valiant            1.0050000  0  0 -0.5000000 -1.5000000

## matrix method
m <- qM(mtcars)
fmean(m)
#>        mpg        cyl       disp         hp       drat         wt       qsec 
#>  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
#>         vs         am       gear       carb 
#>   0.437500   0.406250   3.687500   2.812500 
fmean(m, g)
#>            mpg cyl     disp        hp     drat       wt     qsec vs am     gear
#> 4.0.1 26.00000   4 120.3000  91.00000 4.430000 2.140000 16.70000  0  1 5.000000
#> 4.1.0 22.90000   4 135.8667  84.66667 3.770000 2.935000 20.97000  1  0 3.666667
#> 4.1.1 28.37143   4  89.8000  80.57143 4.148571 2.028286 18.70000  1  1 4.142857
#> 6.0.1 20.56667   6 155.0000 131.66667 3.806667 2.755000 16.32667  0  1 4.333333
#> 6.1.0 19.12500   6 204.5500 115.25000 3.420000 3.388750 19.21500  1  0 3.500000
#> 8.0.0 15.05000   8 357.6167 194.16667 3.120833 4.104083 17.14250  0  0 3.000000
#> 8.0.1 15.40000   8 326.0000 299.50000 3.880000 3.370000 14.55000  0  1 5.000000
#>           carb
#> 4.0.1 2.000000
#> 4.1.0 1.666667
#> 4.1.1 1.428571
#> 6.0.1 4.666667
#> 6.1.0 2.500000
#> 8.0.0 3.083333
#> 8.0.1 6.000000
head(fmean(m, g, TRA = "-")) # etc..
#>                          mpg cyl      disp        hp        drat         wt
#> Mazda RX4          0.4333333   0  5.000000 -21.66667  0.09333333 -0.1350000
#> Mazda RX4 Wag      0.4333333   0  5.000000 -21.66667  0.09333333  0.1200000
#> Datsun 710        -5.5714286   0 18.200000  12.42857 -0.29857143  0.2917143
#> Hornet 4 Drive     2.2750000   0 53.450000  -5.25000 -0.34000000 -0.1737500
#> Hornet Sportabout  3.6500000   0  2.383333 -19.16667  0.02916667 -0.6640833
#> Valiant           -1.0250000   0 20.450000 -10.25000 -0.66000000  0.0712500
#>                         qsec vs am       gear       carb
#> Mazda RX4          0.1333333  0  0 -0.3333333 -0.6666667
#> Mazda RX4 Wag      0.6933333  0  0 -0.3333333 -0.6666667
#> Datsun 710        -0.0900000  0  0 -0.1428571 -0.4285714
#> Hornet 4 Drive     0.2250000  0  0 -0.5000000 -1.5000000
#> Hornet Sportabout -0.1225000  0  0  0.0000000 -1.0833333
#> Valiant            1.0050000  0  0 -0.5000000 -1.5000000

## method for grouped data frames - created with dplyr::group_by or fgroup_by
mtcars |> fgroup_by(cyl,vs,am) |> fmean()         # Ordinary
#>   cyl vs am      mpg     disp        hp     drat       wt     qsec     gear
#> 1   4  0  1 26.00000 120.3000  91.00000 4.430000 2.140000 16.70000 5.000000
#> 2   4  1  0 22.90000 135.8667  84.66667 3.770000 2.935000 20.97000 3.666667
#> 3   4  1  1 28.37143  89.8000  80.57143 4.148571 2.028286 18.70000 4.142857
#> 4   6  0  1 20.56667 155.0000 131.66667 3.806667 2.755000 16.32667 4.333333
#> 5   6  1  0 19.12500 204.5500 115.25000 3.420000 3.388750 19.21500 3.500000
#> 6   8  0  0 15.05000 357.6167 194.16667 3.120833 4.104083 17.14250 3.000000
#> 7   8  0  1 15.40000 326.0000 299.50000 3.880000 3.370000 14.55000 5.000000
#>       carb
#> 1 2.000000
#> 2 1.666667
#> 3 1.428571
#> 4 4.666667
#> 5 2.500000
#> 6 3.083333
#> 7 6.000000
mtcars |> fgroup_by(cyl,vs,am) |> fmean(hp)       # Weighted
#>   cyl vs am sum.hp      mpg      disp     drat       wt     qsec     gear
#> 1   4  0  1     91 26.00000 120.30000 4.430000 2.140000 16.70000 5.000000
#> 2   4  1  0    254 22.69409 134.33504 3.779843 2.898169 21.08846 3.618110
#> 3   4  1  1    564 27.68209  93.87482 4.080266 2.067223 18.54041 4.200355
#> 4   6  0  1    395 20.42405 153.35443 3.775949 2.757468 16.19063 4.443038
#> 5   6  1  0    461 19.10087 202.24425 3.455358 3.390868 19.16941 3.533623
#> 6   8  0  0   2330 14.82854 363.10815 3.143090 4.158685 17.08489 3.000000
#> 7   8  0  1    599 15.35259 323.03673 3.839699 3.393706 14.55593 5.000000
#>       carb
#> 1 2.000000
#> 2 1.618110
#> 3 1.485816
#> 4 4.886076
#> 5 2.600868
#> 6 3.210300
#> 7 6.237062
mtcars |> fgroup_by(cyl,vs,am) |> fmean(hp, "-")  # Weighted Transform
#>                     cyl vs am  hp         mpg       disp          drat
#> Mazda RX4             6  0  1 110  0.57594937   6.645570  0.1240506329
#> Mazda RX4 Wag         6  0  1 110  0.57594937   6.645570  0.1240506329
#> Datsun 710            4  1  1  93 -4.88209220  14.125177 -0.2302659574
#> Hornet 4 Drive        6  1  0 110  2.29913232  55.755748 -0.3753579176
#> Hornet Sportabout     8  0  0 175  3.87145923  -3.108155  0.0069098712
#> Valiant               6  1  0 105 -1.00086768  22.755748 -0.6953579176
#> Duster 360            8  0  0 245 -0.52854077  -3.108155  0.0669098712
#> Merc 240D             4  1  0  62  1.70590551  12.364961 -0.0898425197
#> Merc 230              4  1  0  95  0.10590551   6.464961  0.1401574803
#> Merc 280              6  1  0 123  0.09913232 -34.644252  0.4646420824
#> Merc 280C             6  1  0 123 -1.30086768 -34.644252  0.4646420824
#> Merc 450SE            8  0  0 180  1.57145923 -87.308155 -0.0730901288
#> Merc 450SL            8  0  0 180  2.47145923 -87.308155 -0.0730901288
#> Merc 450SLC           8  0  0 180  0.37145923 -87.308155 -0.0730901288
#> Cadillac Fleetwood    8  0  0 205 -4.42854077 108.891845 -0.2130901288
#> Lincoln Continental   8  0  0 215 -4.42854077  96.891845 -0.1430901288
#> Chrysler Imperial     8  0  0 230 -0.12854077  76.891845  0.0869098712
#> Fiat 128              4  1  1  66  4.71790780 -15.174823 -0.0002659574
#> Honda Civic           4  1  1  52  2.71790780 -18.174823  0.8497340426
#> Toyota Corolla        4  1  1  65  6.21790780 -22.774823  0.1397340426
#> Toyota Corona         4  1  0  97 -1.19409449 -14.235039 -0.0798425197
#> Dodge Challenger      8  0  0 150  0.67145923 -45.108155 -0.3830901288
#> AMC Javelin           8  0  0 150  0.37145923 -59.108155  0.0069098712
#> Camaro Z28            8  0  0 245 -1.52854077 -13.108155  0.5869098712
#> Pontiac Firebird      8  0  0 175  4.37145923  36.891845 -0.0630901288
#> Fiat X1-9             4  1  1  66 -0.38209220 -14.874823 -0.0002659574
#> Porsche 914-2         4  0  1  91  0.00000000   0.000000  0.0000000000
#> Lotus Europa          4  1  1 113  2.71790780   1.225177 -0.3102659574
#> Ford Pantera L        8  0  1 264  0.44741235  27.963272  0.3803005008
#> Ferrari Dino          6  0  1 175 -0.72405063  -8.354430 -0.1559493671
#> Maserati Bora         8  0  1 335 -0.35258765 -22.036728 -0.2996994992
#> Volvo 142E            4  1  1 109 -6.28209220  27.125177  0.0297340426
#>                              wt        qsec       gear       carb
#> Mazda RX4           -0.13746835  0.26936709 -0.4430380 -0.8860759
#> Mazda RX4 Wag        0.11753165  0.82936709 -0.4430380 -0.8860759
#> Datsun 710           0.25277660  0.06959220 -0.2003546 -0.4858156
#> Hornet 4 Drive      -0.17586768  0.27058568 -0.5336226 -1.6008677
#> Hornet Sportabout   -0.71868455 -0.06489270  0.0000000 -1.2103004
#> Valiant              0.06913232  1.05058568 -0.5336226 -1.6008677
#> Duster 360          -0.58868455 -1.24489270  0.0000000  0.7896996
#> Merc 240D            0.29183071 -1.08846457  0.3818898  0.3818898
#> Merc 230             0.25183071  1.81153543  0.3818898  0.3818898
#> Merc 280             0.04913232 -0.86941432  0.4663774  1.3991323
#> Merc 280C            0.04913232 -0.26941432  0.4663774  1.3991323
#> Merc 450SE          -0.08868455  0.31510730  0.0000000 -0.2103004
#> Merc 450SL          -0.42868455  0.51510730  0.0000000 -0.2103004
#> Merc 450SLC         -0.37868455  0.91510730  0.0000000 -0.2103004
#> Cadillac Fleetwood   1.09131545  0.89510730  0.0000000  0.7896996
#> Lincoln Continental  1.26531545  0.73510730  0.0000000  0.7896996
#> Chrysler Imperial    1.18631545  0.33510730  0.0000000  0.7896996
#> Fiat 128             0.13277660  0.92959220 -0.2003546 -0.4858156
#> Honda Civic         -0.45222340 -0.02040780 -0.2003546  0.5141844
#> Toyota Corolla      -0.23222340  1.35959220 -0.2003546 -0.4858156
#> Toyota Corona       -0.43316929 -1.07846457 -0.6181102 -0.6181102
#> Dodge Challenger    -0.63868455 -0.21489270  0.0000000 -1.2103004
#> AMC Javelin         -0.72368455  0.21510730  0.0000000 -1.2103004
#> Camaro Z28          -0.31868455 -1.67489270  0.0000000  0.7896996
#> Pontiac Firebird    -0.31368455 -0.03489270  0.0000000 -1.2103004
#> Fiat X1-9           -0.13222340  0.35959220 -0.2003546 -0.4858156
#> Porsche 914-2        0.00000000  0.00000000  0.0000000  0.0000000
#> Lotus Europa        -0.55422340 -1.64040780  0.7996454  0.5141844
#> Ford Pantera L      -0.22370618 -0.05592654  0.0000000 -2.2370618
#> Ferrari Dino         0.01253165 -0.69063291  0.5569620  1.1139241
#> Maserati Bora        0.17629382  0.04407346  0.0000000  1.7629382
#> Volvo 142E           0.71277660  0.05959220 -0.2003546  0.5141844
#> 
#> Grouped by:  cyl, vs, am  [7 | 5 (3.8) 1-12] 
mtcars |> fgroup_by(cyl,vs,am) |>
               fselect(mpg,hp) |> fmean(hp, "-")  # Only mpg
#>                      hp         mpg
#> Mazda RX4           110  0.57594937
#> Mazda RX4 Wag       110  0.57594937
#> Datsun 710           93 -4.88209220
#> Hornet 4 Drive      110  2.29913232
#> Hornet Sportabout   175  3.87145923
#> Valiant             105 -1.00086768
#> Duster 360          245 -0.52854077
#> Merc 240D            62  1.70590551
#> Merc 230             95  0.10590551
#> Merc 280            123  0.09913232
#> Merc 280C           123 -1.30086768
#> Merc 450SE          180  1.57145923
#> Merc 450SL          180  2.47145923
#> Merc 450SLC         180  0.37145923
#> Cadillac Fleetwood  205 -4.42854077
#> Lincoln Continental 215 -4.42854077
#> Chrysler Imperial   230 -0.12854077
#> Fiat 128             66  4.71790780
#> Honda Civic          52  2.71790780
#> Toyota Corolla       65  6.21790780
#> Toyota Corona        97 -1.19409449
#> Dodge Challenger    150  0.67145923
#> AMC Javelin         150  0.37145923
#> Camaro Z28          245 -1.52854077
#> Pontiac Firebird    175  4.37145923
#> Fiat X1-9            66 -0.38209220
#> Porsche 914-2        91  0.00000000
#> Lotus Europa        113  2.71790780
#> Ford Pantera L      264  0.44741235
#> Ferrari Dino        175 -0.72405063
#> Maserati Bora       335 -0.35258765
#> Volvo 142E          109 -6.28209220
#> 
#> Grouped by:  cyl, vs, am  [7 | 5 (3.8) 1-12]