wtd.colMeans2: Weighted Mean of each Column - WORK IN PROGRESS

View source: R/wtd.colMeans2.R

wtd.colMeans2R Documentation

Weighted Mean of each Column - WORK IN PROGRESS

Description

Returns weighted mean of each column of a data.frame or matrix, based on specified weights, one weight per row. But also see data.table::data.table() used for wtd.colMeans()

Usage

wtd.colMeans2(x, wts, by = NULL, na.rm = FALSE, dims = 1)

Arguments

x

Data.frame or matrix, required.

wts

Weights, optional, defaults to nothing i.e. unweighted, and if specified must be vector of weights recycled to be same length as NROW(x) # not the name of the weights field in data.frame x, as single character string, e.g., "weightcol"

by

Optional vector, default is none, that can provide a single column name (as character) or character vector of column names,

na.rm

Logical value, optional, TRUE by default. Defines whether NA values should be removed before result is found. Otherwise result will be NA when any NA is in a vector.

dims

dims=1 is default. Not used. integer: Which dimensions are regarded as 'rows' or 'columns' to sum over. For row, the sum or mean is over dimensions dims+1, ...; for col* it is over dimensions 1:dims.

Value

Returns a vector of numbers of length equal to number of columns in df.

See Also

wtd.colMeans() wtd.rowMeans() wtd.rowSums() rowMaxs() rowMins() colMins()

Examples

x=data.frame(a=c(NA, 2:10), b=rep(100,10), c=rep(3,10))
w=c(1.1, 2, NA)
cbind(x, wtd.rowMeans(x, w) )
cbind(x, wtd.rowSums(x, w) )
x=data.frame(a=c(NA, 2:4), b=rep(100,4), c=rep(3,4))
w=c(1.1, 2, NA, 0)
print(cbind(x,w, wtd=w*x))
print(wtd.colMeans(x, w, na.rm=TRUE))
#rbind(cbind(x,w,wtd=w*x), c(wtd.colMeans(x,w,na.rm=TRUE), 'wtd.colMeans', rep(NA,length(w))))

x=data.frame(a=c(NA, 2:10), b=rep(100,10), c=rep(3,10))
w=c(1.1, 2, NA, rep(1, 7))
print(cbind(x,w, wtd=w*x))
rbind(cbind(x, w), cbind(wtd.colMeans(x, w, na.rm=TRUE), w='wtd.colMeans') )
print(w*cbind(x,w))


ejanalysis/analyze.stuff documentation built on April 2, 2024, 10:10 a.m.