[R] Simple loop

peter dalgaard pdalgd at gmail.com
Sat May 7 09:24:46 CEST 2011


On May 4, 2011, at 17:52 , William Dunlap wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org 
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Petr Savicky
>> Sent: Wednesday, May 04, 2011 12:51 AM
>> To: r-help at r-project.org
>> Subject: Re: [R] Simple loop
>> 
>> On Tue, May 03, 2011 at 12:04:47PM -0700, William Dunlap wrote:
>> [...]
>>> ave() can deal that problem:
>>>> cbind(x, newCol2 = with(x, ave(H, Site, Prof,
>>> FUN=function(y)y-min(y))))
>>>    Site Prof  H newCol2
>>>  1    1    1 24       8
>>>  2    1    1 16       0
>>>  3    1    1 67      51
>>>  4    1    2 23       0
>>>  5    1    2 56      33
>>>  6    1    2 45      22
>>>  7    2    1 67      21
>>>  8    2    1 46       0
>>>  Warning message:
>>>  In min(y) : no non-missing arguments to min; returning Inf
>>> The warning is unfortunate: ave() calls FUN even for when
>>> there is no data for a particular group (Site=2, Prof=2 in this
>>> case).
>> 
>> The warning may be avoided using min(y, Inf) instead of min().
> 
> Yes, but the fact remains that ave() wastes time and causes
> unnecessary warnings and errors by calling FUN when it knows
> it will do nothing with the result (because there are no entries
> in x with a given combination of the factor levels in the ...
> arguments).
> 
> Using paste(Site,Prof) when calling ave() is ugly, in that it
> forces you to consider implementation details that you expect
> ave() to take care of (how does paste convert various types
> to strings?).  It also courts errors  since paste("A B", "C")
> and paste("A", "B C") give the same result but represent different
> Site/Prof combinations.

Well, ave() uses interaction(...) and interaction() has a "drop" argument, so

> with(x, ave(H, Site, Prof, drop=TRUE, FUN=function(y)y-min(y)))
[1]  8  0 51  0 33 22 21  0

(I suppose ?ave should be a bit more explicit about passing "...")

> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com 
> 
>> 
>>  cbind(x, newCol2 = with(x, ave(H, Site, Prof, 
>> FUN=function(y)y-min(y,Inf))))
>> 
>>    Site Prof  H newCol2
>>  1    1    1 24       8
>>  2    1    1 16       0
>>  3    1    1 67      51
>>  4    1    2 23       0
>>  5    1    2 56      33
>>  6    1    2 45      22
>>  7    2    1 67      21
>>  8    2    1 46       0
>> 
>> Another approach is to combine Site, Prof to a single column
>> in any way suitable for the application. For example
>> 
>>  cbind(x, newCol2 = with(x, ave(H, paste(Site, Prof), 
>> FUN=function(y)y-min(y))))
>> 
>> Petr Savicky.
>> 
>> ______________________________________________
>> 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.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list