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 ?
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),
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
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
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.
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/
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
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,
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
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
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
'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
?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
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
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
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
16 matches
Mail list logo