Re: [R] tapply for function taking of 1 argument?

2010-02-04 Thread J. R. M. Hosking
sjaffe wrote: I'm sure I can put this together from the various 'apply's and split, but I wonder if anyone has a quick incantation: E.g. I can do tapply( data, groups, mean) but how can I do something like: tapply( list(data,weights), groups, weighted.mean ) ? (or: mapply is to sapply as ?

Re: [R] tapply for function taking of 1 argument?

2010-02-04 Thread David Winsemius
On Feb 4, 2010, at 9:56 AM, J. R. M. Hosking wrote: sjaffe wrote: I'm sure I can put this together from the various 'apply's and split, but I wonder if anyone has a quick incantation: E.g. I can do tapply( data, groups, mean) but how can I do something like: tapply( list(data,weights),

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread Petr PIKAL
Hi r-help-boun...@r-project.org napsal dne 02.02.2010 22:16:06: 'fraid not :-(( tapply( data, groups, weighted.mean, weights) tapply(seq(along=lll), rrr, function(i, x, w) weighted.mean(x[i], w[i]), x=lll, w=ttt) If you want to subset more than one thing, subset the index

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread Steve Jaffe
Yes, this is clearly the key to working with subsets. Thanks -Original Message- From: Petr PIKAL [mailto:petr.pi...@precheza.cz] Sent: Wednesday, February 03, 2010 4:16 AM To: Steve Jaffe Cc: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument? Hi r-help-boun

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread David Freedman
also, library(plyr) ddply(d,~grp,function(df) weighted.mean(df$x,df$w)) -- View this message in context: http://n4.nabble.com/tapply-for-function-taking-of-1-argument-tp1460392p1461428.html Sent from the R help mailing list archive at Nabble.com.

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread hadley wickham
On Wed, Feb 3, 2010 at 11:06 AM, David Freedman 3.14da...@gmail.com wrote: also, library(plyr) ddply(d,~grp,function(df) weighted.mean(df$x,df$w)) Or ddply(d, grp, summarise, mean = weighted.mean(x, w)) which is convenient if you want more than one output Hadley -- http://had.co.nz/

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread Gabor Grothendieck
Also try this: library(sqldf) DF - data.frame(data = 1:10, groups = rep(1:2, 5), weights = 1) sqldf(select groups, sum(data * weights)/sum(weights) 'wtd mean' from DF group by groups) groups wtd mean 1 15 2 26 On Tue, Feb 2, 2010 at 5:06 PM, sjaffe

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread sjaffe
Thanks, I’m actually more comfortable with vector-ish syntax than sql-ish but this is a good thing to keep in mind… I wonder how it compares in performance versus ‘by’ or ‘tapply’ From: Gabor Grothendieck [via R] [mailto:ml-node+1461531-1948782...@n4.nabble.com] Sent: Wednesday,

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread Bert Gunter
Of sjaffe Sent: Wednesday, February 03, 2010 10:25 AM To: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument? Thanks, Ibm actually more comfortable with vector-ish syntax than sql-ish but this is a good thing to keep in mindb I wonder how it compares in performance versus

Re: [R] tapply for function taking of 1 argument?

2010-02-03 Thread hadley wickham
It will of necessity be slower (because there's more machinery underlying the sqldf package); but I doubt whether it would be noticeably slower than the native R solution in most practical situations. The same would be true for plyR's implementation (it relies on the proto package, which slows

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread Jorge Ivan Velez
Hi sjaffem, You were almost there: tapply( yourdata, groups, weighted.mean, weights) See ?tapply for more information. HTH, Jorge On Tue, Feb 2, 2010 at 3:58 PM, sjaffe wrote: I'm sure I can put this together from the various 'apply's and split, but I wonder if anyone has a quick

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread sjaffe
'fraid not :-(( tapply( data, groups, weighted.mean, weights) won't work because the *entire* weights vector is passed as the 2nd arg to weighted.means. But weighted.mean needs 'weights' to be split in the same way as 'data' -- the first and 2nd args need to correspond. Jorge Ivan Velez

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread Bert Gunter
?by Bert Gunter Genentech Nonclinical Statistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of sjaffe Sent: Tuesday, February 02, 2010 1:16 PM To: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread Charles C. Berry
On Tue, 2 Feb 2010, sjaffe wrote: 'fraid not :-(( tapply( data, groups, weighted.mean, weights) won't work because the *entire* weights vector is passed as the 2nd arg to weighted.means. But weighted.mean needs 'weights' to be split in the same way as 'data' -- the first and 2nd args need to

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread Steve Jaffe
Excellent! I knew there would be a clever answer using 'do.call' :-) -Original Message- From: Charles C. Berry [mailto:cbe...@tajo.ucsd.edu] Sent: Tuesday, February 02, 2010 4:25 PM To: Steve Jaffe Cc: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument

Re: [R] tapply for function taking of 1 argument?

2010-02-02 Thread sjaffe
Thanks! :-) I suppose it's obvious, but one will generally have to use a (anonymous) function to 'unpack' the data.frame into columns, unless the function already knows how to do this. I mention this because when I tested the solution on my example I got an unexpected result -- apparently