[Rd] as.list method for by Objects
hpages at fredhutch.org
Tue Jan 30 23:24:17 CET 2018
I agree that it makes sense to expect as.list() to perform
a "strict coercion" i.e. to return an object of class "list",
*even* on a list derivative. That's what as( , "list") does
# on a data.frame object
as(data.frame(), "list") # object of class "list"
# (but strangely it drops the names)
# on a by object
x <- by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
as(x, "list") # object of class "list"
More generally speaking as() is expected to perform "strict
coercion" by default, unless called with 'strict=FALSE'.
That's also what as.list() does on a data.frame:
as.list(data.frame()) # object of class "list"
FWIW as.numeric() also performs "strict coercion" on an integer
as.numeric(1:3) # object of class "numeric"
So an as.list.env method that does the same as as(x, "list")
would bring a small touch of consistency in an otherwise
quite inconsistent world of coercion methods(*).
(*) as(data.frame(), "list", strict=FALSE) doesn't do what you'd
expect (just one of many examples)
On 01/29/2018 05:00 PM, Dario Strbenac wrote:
> Good day,
> I'd like to suggest the addition of an as.list method for a by object that actually returns a list of class "list". This would make it safer to do type-checking, because is.list also returns TRUE for a data.frame variable and using class(result) == "list" is an alternative that only returns TRUE for lists. It's also confusing initially that
>  "by"
>  TRUE
> since there's no explicit class definition for "by" and no mention if it has any superclasses.
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> R-devel at r-project.org mailing list
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024
E-mail: hpages at fredhutch.org
Phone: (206) 667-5791
Fax: (206) 667-1319
More information about the R-devel