Re: [R] reshape with function(x,y)?

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Mon, 30 May 2011 00:52:18 -0700

If you want the weighted variance added to the data frame, don't melt the data first.

library(Hmisc)
dat<-data.frame(ids=c("A","A","A","B","B","B"),rate=c(12,14,17,10,8,5),

                sample=c(100,80,60,50,40,45))
library(plyr)
library(Hmisc)
ddply(dat, 'ids', transform, wtdvar = wtd.var(rate, sample)) ddply(dat, 'ids', summarise, wtdvar = wtd.var(rate, sample))

Dennis

On Sun, May 29, 2011 at 1:25 PM, John K. Williams <sabesin2001_at_gmail.com> wrote:
> Hi,
> I'm not sure if this is impossible or if I just don't know the syntax.
>
> example:
>
> library(reshape);
> library(corpcor);
>
> data<-data.frame(ids=c("A","A","A","B","B","B"),rate=c(12,14,17,10,8,5),sample=c(100,80,60,50,40,45));
> data_melted<-melt.data.frame(data,id.vars=c("ids"),measure.vars=c("rate","sample"));
>
> #the line below just gives me the var, can I do weighted var weighted by the
> sample size?
> data_reshape<-cast(data_melted,ids~variable,function(x,y) wt.var(x,y));
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 30 May 2011 - 07:54:27 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 30 May 2011 - 08:00:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive