[R] Two-argument functions in tapply()

Michal Bojanowski buoy at wp.pl
Thu Mar 22 15:36:10 CET 2001


Hello to all.

My question is very simple...

Let's say we have a data frame with three variables (columns): X, W and F. X
is a numeric variable (e.g. like income) F is a factor (e.g. with 2 levels)
and W is a case weight (data are from household sample but an individual was
interviewed, weights are functions of number of persons in the house hold).

I wanted to compute a means of X weighted by W within groups defined by F
with the function below:

	wmean <- function(x,w)
		{
		wm <- sum( x*w/sum(w) )
		wm
		}

used within tapply() function, like:

	tapply(x,f,wmean,w=w)

but I received some values with 2 warnings:

	Warning messages:
	1: longer object length
	        is not a multiple of shorter object length in: x * w
	2: longer object length
      	  is not a multiple of shorter object length in: x * w

What is the right way to get the results (how to use 2-argument function in
tapply()), or maybe there is another simple way to do this


ThankYouInAdvance

Michal Bojanowski

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list