# NOT RUN {
## default vector method
mpg <- mtcars$mpg
fmean(mpg)                         # Simple mean
fmean(mpg, w = mtcars$hp)          # Weighted mean: Weighted by hp
fmean(mpg, TRA = "-")              # Simple transformation: demeaning (See also ?W)
fmean(mpg, mtcars$cyl)             # Grouped mean
fmean(mpg, mtcars[8:9])            # another grouped mean.
g <- GRP(mtcars[c(2,8:9)])
fmean(mpg, g)                      # Pre-computing groups speeds up the computation
fmean(mpg, g, mtcars$hp)           # Grouped weighted mean
fmean(mpg, g, TRA = "-")           # Demeaning by group
fmean(mpg, g, mtcars$hp, "-")      # Group-demeaning using weighted group means
## data.frame method
fmean(mtcars)
fmean(mtcars, g)
fmean(fgroup_by(mtcars, cyl, vs, am))  # another way of doing it...
fmean(mtcars, g, TRA = "-") # etc...
## matrix method
m <- qM(mtcars)
fmean(m)
fmean(m, g)
fmean(m, g, TRA = "-") # etc...
## method for grouped tibbles - for use with dplyr
library(dplyr)
mtcars %>% group_by(cyl,vs,am) %>% fmean           # Ordinary
mtcars %>% group_by(cyl,vs,am) %>% fmean(hp)       # Weighted
mtcars %>% group_by(cyl,vs,am) %>% fmean(hp,"-")   # Weighted Transform
mtcars %>% group_by(cyl,vs,am) %>%
           select(mpg,hp) %>% fmean(hp,"-")        # Only mpg
mtcars %>% fgroup_by(cyl,vs,am) %>%              # Equivalent but faster !!
           fselect(mpg,hp) %>% fmean(hp,"-")
# }
Run the code above in your browser using DataLab