Learn R Programming

flashlight (version 0.9.0)

grouped_weighted_mean: Fast Grouped Weighted Mean

Description

Fast version of grouped_stats(..., counts = FALSE). Works if there is at most one "by" variable.

Usage

grouped_weighted_mean(
  data,
  x,
  w = NULL,
  by = NULL,
  na.rm = TRUE,
  value_name = x
)

Value

A data.frame with grouped weighted means.

Arguments

data

A data.frame.

x

Variable name in data to summarize.

w

Optional name of the column in data with case weights.

by

An optional vector of column names in data used to group the results.

na.rm

Should missing values in x be removed?

value_name

Name of the resulting column with means.

Examples

Run this code
n <- 100
data <- data.frame(
  x = rnorm(n),
  w = runif(n),
  group = factor(sample(1:3, n, TRUE))
)
grouped_weighted_mean(data, x = "x", w = "w", by = "group")

Run the code above in your browser using DataLab