[R] tapply for function taking of >1 argument?

Bert Gunter gunter.berton at gene.com
Wed Feb 3 19:38:53 CET 2010


My editorial opinion only:

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
things down a bit).

The point is that the most important issue in almost all cases is the
programmer's time to create and debug correct code, especially as the native
machine speeds continue to increase. R gives you the option to choose
whatever idiom you prefer to minimize this. The software implementation
differences thereafter will rarely be important.

In other words, pick your poison.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
 
 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of sjaffe
Sent: Wednesday, February 03, 2010 10:25 AM
To: r-help at 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 bbyb or btapplyb

From: Gabor Grothendieck [via R]
[mailto:ml-node+1461531-1948782090 at n4.nabble.com]
Sent: Wednesday, February 03, 2010 1:19 PM
To: Steve Jaffe
Subject: Re: tapply for function taking of >1 argument?

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      1        5
2      2        6

On Tue, Feb 2, 2010 at 5:06 PM, sjaffe <[hidden
email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1461531&i=0>>
wrote:

>
> 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 weighted.mean will operate on a 2-column
> dataframe but not in the way one would expect.
>
> data = 1:10
> weights = rep(1,10)
> groups = rep(c(1,2),5)
>  by( data.frame(data,weights), groups, weighted.mean)
> groups: 1
> [1] 15
> ------------------------------------------------------------
> groups: 2
> [1] 17.5
>>
>
>
> But
>
>  by( data.frame(data,weights), groups, function(d) { weighted.mean(d[,1],
> d[,2]) } )
>
> does the right thing
>
> groups: 1
> [1] 5
> ------------------------------------------------------------
> groups: 2
> [1] 6
>>
>
>
>
> Bert Gunter wrote:
>>
>> ?by
>>
>
>> Bert Gunter
>> Genentech Nonclinical Statistics
>>
> --
> View this message in context:
http://n4.nabble.com/tapply-for-function-taking-of-1-argument-tp1460392p1460
489.html
> Sent from the R help mailing list archive at Nabble.com.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden
email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1461531&i=1>
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.
>

______________________________________________
[hidden
email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1461531&i=2>
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.

________________________________
View message @
http://n4.nabble.com/tapply-for-function-taking-of-1-argument-tp1460392p1461
531.html
To unsubscribe from Re: tapply for function taking of >1 argument?, click
here< (link removed) ==>.


-- 
View this message in context:
http://n4.nabble.com/tapply-for-function-taking-of-1-argument-tp1460392p1461
541.html
Sent from the R help mailing list archive at Nabble.com.

	[[alternative HTML version deleted]]



More information about the R-help mailing list