[R] plyr: a*ply with functions that return matrices-- possible bug in aaply?

Michael Friendly friendly at yorku.ca
Wed Oct 6 15:16:21 CEST 2010


On 10/4/2010 9:22 AM, hadley wickham wrote:
>>   That is, I want to define something like the
>> following using an a*ply method, but aaply gives a result in which the
>> applied .margin(s) do not appear last in the
>> result, contrary to the documentation for ?aaply.  I think this is a bug,
>> either in the function or the documentation,
>> but perhaps there's something I misunderstand for this case.
>
> Maybe the documentation isn't clear but I think this is behaving as expected:
>
>   * the margin you split on comes first in the output
>   * followed by the dimensions created by the applied function.
>
> Hadley

OK, this is clear (and more explicit than the .Rd doc description of 
Value).
What is still not clear is why alply() returns the split levels in their 
original order, while aaply() returns them in sorted order when the 
level names are character.  My application is for
contingency tables, where, e.g., a dimension may have levels
c("Lo", "Med", "Hi") and I need to preserve this order in the result.

 > alply(HairEyeColor, 3, fun2way) # gives c("Male", "Female")
$`1`
        Eye
Hair    Brown Blue Hazel
   Black    32   11    10
   Brown    53   50    25
   Red      10   10     7

$`2`
        Eye
Hair    Brown Blue Hazel
   Black    36    9     5
   Brown    66   34    29
   Red      16    7     7

attr(,"split_type")
[1] "array"
attr(,"split_labels")
      Sex
1   Male
2 Female
 >
 > aaply(HairEyeColor, 3, fun2way) # gives c("Female", "Male")
, , Eye = Brown

         Hair
Sex      Black Brown Red
   Female    36    66  16
   Male      32    53  10

, , Eye = Blue

         Hair
Sex      Black Brown Red
   Female     9    34   7
   Male      11    50  10

, , Eye = Hazel

         Hair
Sex      Black Brown Red
   Female     5    29   7
   Male      10    25   7

 >



-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list