Re: [R] tapply and weighted means

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri 13 Jan 2006 - 01:57:58 EST

you need also to split the 'w' column, for each level of 'x'; you could use:

lapply(split(truc, truc$x), function(z) weighted.mean(z$y, z$w))

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



> I' m trying to compute weighted mean on different
> groups but it only returns NA. If I use the following
> data.frame truc:
>
> x y w
> 1 1 1
> 1 2 2
> 1 3 1
> 1 4 2
> 0 2 1
> 0 3 2
> 0 4 1
> 0 5 1
>
> where x is a factor, and then use the command :
>
> tapply(truc$y,list(truc$x),wtd.mean, weights=truc$w)
>
> I just get NA. What's the problem ? What can I do ?
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jan 13 02:05:47 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:03 EST