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> class(x)[1] "by"> is.list(x)[1] 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 Australia
I agree that it would make sense for the object to have c("by", "list") as its class attribute, since the object is known to behave as a list. However, it would may be too disruptive to make this change at this point. Hard to predict. Michael On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <dstr7320 at uni.sydney.edu.au> 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 > > > class(x) > [1] "by" > > is.list(x) > [1] 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 > Australia > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >[[alternative HTML version deleted]]
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 by default: # 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 vector: 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(*). H. (*) 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 > >> class(x) > [1] "by" >> is.list(x) > [1] 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 > Australia > > ______________________________________________ > R-devel at r-project.org mailing list > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e>-- Herv? Pag?s 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
On 01/30/2018 02:24 PM, Herv? Pag?s wrote:> 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 > by default: > > ? # 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 > vector: > > ? as.numeric(1:3)? # object of class "numeric" > > So an as.list.env method that does the same as as(x, "list")^^^^^^^^^^^ oops, I meant as.list.by, sorry... H.> would bring a small touch of consistency in an otherwise > quite inconsistent world of coercion methods(*). > > H. > > (*) 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 >> >>> class(x) >> [1] "by" >>> is.list(x) >> [1] 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 >> Australia >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbMrKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1dajp8RLURfRz0-nbwH721jFAcEo&e= >> >> >-- Herv? Pag?s 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
Dario, What version of R are you using. In my mildly old 3.4.0 installation and in the version of Revel I have lying around (also mildly old...) I don't see the behavior I think you are describing> b = by(1:2, 1:2, identity)> class(as.list(b))[1] "list"> sessionInfo()R Under development (unstable) (2017-12-19 r73926) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: OS X El Capitan 10.11.6 Matrix products: default BLAS: /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRblas.dylib LAPACK: /Users/beckerg4/local/Rdevel/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.5.0>As for by not having a class definition, no S3 class has an explicit definition, so this is somewhat par for the course here... did I misunderstand something? ~G On Tue, Jan 30, 2018 at 2:24 PM, Herv? Pag?s <hpages at fredhutch.org> wrote:> 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 > by default: > > # 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 > vector: > > 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(*). > > H. > > (*) 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 >> >> class(x) >>> >> [1] "by" >> >>> is.list(x) >>> >> [1] 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 >> Australia >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.et >> hz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84V >> tBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=8nXbM >> rKus1XsG7MluCRy3sluJKKhMVwOPHtudDpYJ4o&s=qDnEZOWalov3E9h1daj >> p8RLURfRz0-nbwH721jFAcEo&e>> >> > -- > Herv? Pag?s > > 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 > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Gabriel Becker, PhD Scientist (Bioinformatics) Genentech Research [[alternative HTML version deleted]]
>>>>> Michael Lawrence <lawrence.michael at gene.com> >>>>> on Tue, 30 Jan 2018 10:37:38 -0800 writes:> I agree that it would make sense for the object to have c("by", "list") as > its class attribute, since the object is known to behave as a list. Well, but that (list behavior) applies to most non-simple S3 classed objects, say "data.frame", say "lm" to start with real basic ones. The later part of the discussion, seems more relevant to me. Adding "list" to the class attribute seems as wrong to me as e.g. adding "double" to "Date" or "POSIXct" (and many more such cases). For the present case, we should stay with focusing on is.list() being true after as.list() .. the same we would do with as.numeric() and is.numeric(). Martin > However, it would may be too disruptive to make this change at this point. > Hard to predict. > Michael > On Mon, Jan 29, 2018 at 5:00 PM, Dario Strbenac <dstr7320 at uni.sydney.edu.au> > 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 >> >> > class(x) >> [1] "by" >> > is.list(x) >> [1] 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 >> Australia >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> >> > [[alternative HTML version deleted]] > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel