[Rd] apply() returning a list?

David Winsemius dwinsemius at comcast.net
Wed Jul 20 17:13:22 CEST 2011


On Jul 19, 2011, at 7:34 PM, David A. Johnston wrote:

> Hi devel list,
>
> I searched for past posts about this issue and found none, so my  
> apologies
> if this has been addressed before.
>
> The sapply() function has an argument 'simplify', and the mapply()  
> function
> has an argument 'SIMPLIFY'.  I am surprised that the apply()  
> argument does
> not have a similar argument.  Is there a reason for this?
>
> Here's a simple example:
>> x = matrix(1:12, 3, 4)
>> apply(x, 1, sum)
> [1] 22 26 30
>
> This is what I would like to see:
>> apply(x, 1, sum, simplify = FALSE)
> [[1]]
> [1] 22
>
> [[2]]
> [1] 26
>
> [[3]]
> [1] 30
>

Probably no one thought it was that difficult to type the more concise  
and equally expressive:

 > as.list(apply(x, 1, sum))
[[1]]
[1] 22

[[2]]
[1] 26

[[3]]
[1] 30

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-devel mailing list