Tal Galili
2017-Feb-04 12:17 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
In the help page of ?tapply it says that the first argument (X) is "an atomic object, typically a vector." However, tapply seems to be able to handle list objects. For example: ################### l <- as.list(1:10) is.atomic(l) # FALSE index <- c(rep(1,5),rep(2,5)) tapply(l,index,unlist)> tapply(l,index,unlist)$`1` [1] 1 2 3 4 5 $`2` [1] 6 7 8 9 10 ################### Hence, does it mean a list an atomic object? (which I thought it wasn't) or is the help for tapply needs updating? (or some third option I'm missing?) Thanks. ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili at gmail.com | Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]
Bert Gunter
2017-Feb-14 19:05 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
Did you ever receive a reply to this? Note that for your example:> tapply(l,index,sum)Error in FUN(X[[i]], ...) : invalid 'type' (list) of argument A list is definitely not atomic (is.recursive(l) ). So it looks like a "quirk" that FUN = unlist doesn't raise an error. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Feb 4, 2017 at 4:17 AM, Tal Galili <tal.galili at gmail.com> wrote:> In the help page of ?tapply it says that the first argument (X) is "an > atomic object, typically a vector." > > However, tapply seems to be able to handle list objects. For example: > > ################### > > l <- as.list(1:10) > is.atomic(l) # FALSE > index <- c(rep(1,5),rep(2,5)) > tapply(l,index,unlist) > >> tapply(l,index,unlist) > $`1` > [1] 1 2 3 4 5 > > $`2` > [1] 6 7 8 9 10 > > > ################### > > Hence, does it mean a list an atomic object? (which I thought it wasn't) or > is the help for tapply needs updating? > (or some third option I'm missing?) > > Thanks. > > > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hervé Pagès
2017-Feb-15 01:10 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
Hi, tapply() will work on any object 'X' that has a length and supports single-bracket subsetting. These objects are sometimes called "vector-like" objects. Atomic vectors, lists, S4 objects with a "length" and "[" method, etc... are examples of "vector-like" objects. So instead of saying X: an atomic object, typically a vector. I think it would be more accurate if the man page was saying something like X: a vector-like object that supports subsetting with `[`, typically an atomic vector. H. On 02/04/2017 04:17 AM, Tal Galili wrote:> In the help page of ?tapply it says that the first argument (X) is "an > atomic object, typically a vector." > > However, tapply seems to be able to handle list objects. For example: > > ################### > > l <- as.list(1:10) > is.atomic(l) # FALSE > index <- c(rep(1,5),rep(2,5)) > tapply(l,index,unlist) > >> tapply(l,index,unlist) > $`1` > [1] 1 2 3 4 5 > > $`2` > [1] 6 7 8 9 10 > > > ################### > > Hence, does it mean a list an atomic object? (which I thought it wasn't) or > is the help for tapply needs updating? > (or some third option I'm missing?) > > Thanks. > > > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- 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
Bert Gunter
2017-Feb-15 01:28 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
Herv?: Kindly explain this, then:> l <- as.list(1:10) > is.atomic(l) # FALSE[1] FALSE> index <- c(rep(1,5),rep(2,5)) > > > tapply(l,index,unlist)$`1` [1] 1 2 3 4 5 $`2` [1] 6 7 8 9 10> > ## But > > tapply(l,index, sum)Error in FUN(X[[i]], ...) : invalid 'type' (list) of argument Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Feb 14, 2017 at 5:10 PM, Herv? Pag?s <hpages at fredhutch.org> wrote:> Hi, > > tapply() will work on any object 'X' that has a length and supports > single-bracket subsetting. These objects are sometimes called > "vector-like" objects. Atomic vectors, lists, S4 objects with a "length" > and "[" method, etc... are examples of "vector-like" objects. > > So instead of saying > > X: an atomic object, typically a vector. > > I think it would be more accurate if the man page was saying something > like > > X: a vector-like object that supports subsetting with `[`, typically > an atomic vector. > > H. > > On 02/04/2017 04:17 AM, Tal Galili wrote: >> >> In the help page of ?tapply it says that the first argument (X) is "an >> atomic object, typically a vector." >> >> However, tapply seems to be able to handle list objects. For example: >> >> ################### >> >> l <- as.list(1:10) >> is.atomic(l) # FALSE >> index <- c(rep(1,5),rep(2,5)) >> tapply(l,index,unlist) >> >>> tapply(l,index,unlist) >> >> $`1` >> [1] 1 2 3 4 5 >> >> $`2` >> [1] 6 7 8 9 10 >> >> >> ################### >> >> Hence, does it mean a list an atomic object? (which I thought it wasn't) >> or >> is the help for tapply needs updating? >> (or some third option I'm missing?) >> >> Thanks. >> >> >> >> >> >> ----------------Contact >> Details:------------------------------------------------------- >> Contact me: Tal.Galili at gmail.com | >> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >> www.r-statistics.com (English) >> >> ---------------------------------------------------------------------------------------------- >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > -- > 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-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hadley Wickham
2017-Feb-15 19:32 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
It seems like this should be consistent with split(), since that's what actually powers the behaviour. Reading the description for split leads to this rather interesting example: tapply(mtcars, 1:11, I) Hadley On Tue, Feb 14, 2017 at 7:10 PM, Herv? Pag?s <hpages at fredhutch.org> wrote:> Hi, > > tapply() will work on any object 'X' that has a length and supports > single-bracket subsetting. These objects are sometimes called > "vector-like" objects. Atomic vectors, lists, S4 objects with a "length" > and "[" method, etc... are examples of "vector-like" objects. > > So instead of saying > > X: an atomic object, typically a vector. > > I think it would be more accurate if the man page was saying something > like > > X: a vector-like object that supports subsetting with `[`, typically > an atomic vector. > > H. > > > On 02/04/2017 04:17 AM, Tal Galili wrote: >> >> In the help page of ?tapply it says that the first argument (X) is "an >> atomic object, typically a vector." >> >> However, tapply seems to be able to handle list objects. For example: >> >> ################### >> >> l <- as.list(1:10) >> is.atomic(l) # FALSE >> index <- c(rep(1,5),rep(2,5)) >> tapply(l,index,unlist) >> >>> tapply(l,index,unlist) >> >> $`1` >> [1] 1 2 3 4 5 >> >> $`2` >> [1] 6 7 8 9 10 >> >> >> ################### >> >> Hence, does it mean a list an atomic object? (which I thought it wasn't) >> or >> is the help for tapply needs updating? >> (or some third option I'm missing?) >> >> Thanks. >> >> >> >> >> >> ----------------Contact >> Details:------------------------------------------------------- >> Contact me: Tal.Galili at gmail.com | >> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >> www.r-statistics.com (English) >> >> ---------------------------------------------------------------------------------------------- >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > -- > 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-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- http://hadley.nz
Martin Maechler
2017-Feb-20 13:31 UTC
[R] Is a list an atomic object? (or is there an issue with the help page of ?tapply ?)
>>>>> Herv? Pag?s <hpages at fredhutch.org> >>>>> on Tue, 14 Feb 2017 17:10:05 -0800 writes:> Hi, tapply() will work on any object 'X' that has a length > and supports single-bracket subsetting. These objects are > sometimes called "vector-like" objects. Atomic vectors, > lists, S4 objects with a "length" and "[" method, > etc... are examples of "vector-like" objects. > So instead of saying > X: an atomic object, typically a vector. > I think it would be more accurate if the man page was > saying something like > X: a vector-like object that supports subsetting with > `[`, typically an atomic vector. Thank you, Herv?! Actually (someone else mentioned ?) only length(X) and split(X, <group>) need to work, and as split() itself is an S3 generic function, X can be even more general... well depending on how exactly you understand "vector-like". So I would go with X: an R object for which a ?split? method exists. Typically vector-like, allowing subsetting with ?[?. Martin > H. > On 02/04/2017 04:17 AM, Tal Galili wrote: >> In the help page of ?tapply it says that the first >> argument (X) is "an atomic object, typically a vector." >> >> However, tapply seems to be able to handle list >> objects. For example: >> >> ################### >> >> l <- as.list(1:10) is.atomic(l) # FALSE index <- >> c(rep(1,5),rep(2,5)) tapply(l,index,unlist) >> >>> tapply(l,index,unlist) >> $`1` [1] 1 2 3 4 5 >> >> $`2` [1] 6 7 8 9 10 >> >> >> ################### >> >> Hence, does it mean a list an atomic object? (which I >> thought it wasn't) or is the help for tapply needs >> updating? (or some third option I'm missing?) >> >> Thanks.