[Rd] value returned by by()

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Sep 15 15:15:27 CEST 2010



On 15.09.2010 15:00, Seb wrote:
> On Wed, 15 Sep 2010 11:29:23 +0200,
> peter dalgaard<pdalgd at gmail.com>  wrote:
>
>> On Sep 15, 2010, at 10:55 , Uwe Ligges wrote:
>
>>>
>>>
>>> On 14.09.2010 20:50, Seb wrote:
>>>> On Tue, 14 Sep 2010 12:02:04 +0200,
>>>> Uwe Ligges<ligges at statistik.tu-dortmund.de>   wrote:
>>>>
>>>>> It returns a list with athe class attribut set to "by", just use: x<-
>>>>> by(.....)  unclass(x)
>>>>
>>>> Thanks Uwe, however, that still returns an array when using the
>>>> data.frame method for by():
>>>>
>>>> R>   class(unclass(with(warpbreaks, by(warpbreaks[, 1:2], tension, summary))))
>>>> [1] "array"
>>>>
>>>> It seems as if the only way to really ensure a list:
>>>>
>>>> R>   class(lapply(unclass(with(warpbreaks, by(warpbreaks[, 1:2], tension, summary))), function(x) x))
>>>> [1] "list"
>>>>
>>>> but it seems like a waste to call another function just to do this.
>>>>
>>>>
>>>
>>> Then you could still do
>>>
>>> x<- by(.....)
>>> attributes(x)<- NULL
>>>
>
>> Or just use c() instead of unclass(). (The root cause is that even with simplify=FALSE, tapply() will always create an array, in this case a 1d array with dim=3. The _contents_ of the array will be a list, though.)
>
>> Notice that in the relevant cases, what you get really _is_ a list, and both walks and quacks like one.  E.g.
>
>>> L<- with(warpbreaks, by(warpbreaks[, 1], tension, mean, simplify=FALSE))
>>> is.list(L)
>> [1] TRUE
>>> L$M
>> [1] 26.38889
>
> But if one tries to include this list dressed in 'by' clothes into an S4
> class slot declared as a list, then we have problems.  In that case, I
> propose this simple patch to by.Rd, which simply removes the statement
> about the result being *always* a list.
>
>
> Index: by.Rd
> ===================================================================
> --- by.Rd	(revision 52375)
> +++ by.Rd	(working copy)
> @@ -36,8 +36,6 @@
>   }
>   \value{
>     An object of class \code{"by"}, giving the results for each subset.
> -  This is always a list if \code{simplify} is false, otherwise a list or
> -  array (see \code{\link{tapply}}).
>   }
>   \seealso{\code{\link{tapply}}}
>
>
> Thanks!
>

Why? It is still accessible as a list, even with S4 object, at least for 
the cases I tried.

Uwe Ligges



More information about the R-devel mailing list