c_wmean: c_wmean() computes the cluster weighted mean for a given...

View source: R/c_wmean.R

c_wmeanR Documentation

c_wmean() computes the cluster weighted mean for a given variable, and a given clustering variable

Description

c_wmean() computes the cluster weighted mean for a given variable, and a given clustering variable

Usage

c_wmean(x, w, j)

Arguments

x

a numeric vector of a single variable

w

a numeric vector for within cluster weights

j

a numeric vector for clusters index

Details

simple wrapper for ave(x,j), to estimate cluster means weighted by within cluster weights

Value

a numeric vector

Examples

library(dplyr)
library(dplyr)
example_data <- data.frame(
x = c(1,2,3,4,5,6),
w = c(.4,.4,.4,.6,.6,.6),
id_j = c(1,1,1,2,2,2)
)
example_data %>%
mutate(cluster_mean =  c_wmean(x, w, id_j)) %>%
select(x, id_j, cluster_mean) %>%
unique() %>%
print

dacarras/r4sda documentation built on Nov. 9, 2023, 10:17 a.m.