Splits the data into subsets (according to the levels of a factor) and computes the weighted mean for each

agg.wtd.mean(x, by, weights = NULL, na.rm = FALSE)

Arguments

x

numeric vector or data frame of numeric vectors

by

factor used to split the data

weights

numeric vector of weights. If NULL (default), uniform weights (i.e. all equal to 1) are used.

na.rm

logical, indicating whether NA values should be silently removed before the computation proceeds. Default is FALSE.

Value

a data frame

Author

Nicolas Robette

See also

Examples

data(Movies)
agg.wtd.mean(Movies[,c("Critics", "BoxOffice")], by = Movies$Country)
#>         Critics BoxOffice
#> Europe 2.886343  783122.5
#> France 2.923361  327518.2
#> Other  3.041026  428050.1
#> USA    2.686700  838562.2