peter dalgaard
2014-Dec-25 11:02 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
> On 25 Dec 2014, at 08:15 , Mike Miller <mbmiller+l at gmail.com> wrote: > >> >> "is.vector returns TRUE if x is a vector of the specified mode having >> no attributes other than names. It returns FALSE otherwise." > > So that means that a vector in R has no attributes other than names.Wrong. Read carefully. There are - vectors - vectors having no attributes other than names -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Mike Miller
2014-Dec-25 18:57 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, peter dalgaard wrote:> >> On 25 Dec 2014, at 08:15 , Mike Miller <mbmiller+l at gmail.com> wrote: >> >>> >>> "is.vector returns TRUE if x is a vector of the specified mode having >>> no attributes other than names. It returns FALSE otherwise." >> >> So that means that a vector in R has no attributes other than names. > > Wrong. Read carefully. There are > > - vectors > - vectors having no attributes other than namesYou are right. I was being difficult about the meaning of "is.vector()". But would you also say that a matrix is a vector? I was going to ask a question about it how to test that an object is a vector, but then I found this: "is.vector() does not test if an object is a vector. Instead it returns TRUE only if the object is a vector with no attributes apart from names. Use is.atomic(x) || is.list(x) to test if an object is actually a vector.">From here:http://adv-r.had.co.nz/Data-structures.html#vectors> a <- c(1,2,3,4)> names(a) <- LETTERS[1:4]> attr(a, "vecinfo") <- "yes, I'm a vector"> aA B C D 1 2 3 4 attr(,"vecinfo") [1] "yes, I'm a vector"> attributes(a)$names [1] "A" "B" "C" "D" $vecinfo [1] "yes, I'm a vector"> is.vector(a)[1] FALSE> is.atomic(a) || is.list(a)[1] TRUE But then we also see this:> b <- matrix(1:4, 2,2)> is.atomic(b) || is.list(b)[1] TRUE "It is common to call the atomic types ?atomic vectors?, but note that is.vector imposes further restrictions: an object can be atomic but not a vector (in that sense)." https://stat.ethz.ch/R-manual/R-devel/library/base/html/is.recursive.html I think a matrix is always atomic. So a matrix is "not a vector (in that sense)," but "is.matrix returns TRUE if x is a vector and has a 'dim' attribute of length 2." I do think I get what is going on with this, but why should I buy into this conceptualization? Why is it better to say that a matrix *is* a vector than to say that a matrix *contains* a vector? The latter seems to be the more common way of thinking but such things. Even in R you've had to construct two different definitions of "vector" to deal with the inconsistency created by the "matrix is a vector" way of thinking. So there must be something really good about it that I am not understanding (and I'm not being facetious or ironic!) Mike
Duncan Murdoch
2014-Dec-25 20:41 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On 25/12/2014 1:57 PM, Mike Miller wrote:> On Thu, 25 Dec 2014, peter dalgaard wrote: > >> >>> On 25 Dec 2014, at 08:15 , Mike Miller <mbmiller+l at gmail.com> wrote: >>> >>>> >>>> "is.vector returns TRUE if x is a vector of the specified mode having >>>> no attributes other than names. It returns FALSE otherwise." >>> >>> So that means that a vector in R has no attributes other than names. >> >> Wrong. Read carefully. There are >> >> - vectors >> - vectors having no attributes other than names > > You are right. I was being difficult about the meaning of "is.vector()". > > But would you also say that a matrix is a vector? > > I was going to ask a question about it how to test that an object is a > vector, but then I found this: > > "is.vector() does not test if an object is a vector. Instead it returns > TRUE only if the object is a vector with no attributes apart from names. > Use is.atomic(x) || is.list(x) to test if an object is actually a vector." > > From here: > > http://adv-r.had.co.nz/Data-structures.html#vectors > >> a <- c(1,2,3,4) > >> names(a) <- LETTERS[1:4] > >> attr(a, "vecinfo") <- "yes, I'm a vector" > >> a > A B C D > 1 2 3 4 > attr(,"vecinfo") > [1] "yes, I'm a vector" > >> attributes(a) > $names > [1] "A" "B" "C" "D" > > $vecinfo > [1] "yes, I'm a vector" > >> is.vector(a) > [1] FALSE > >> is.atomic(a) || is.list(a) > [1] TRUE > > But then we also see this: > >> b <- matrix(1:4, 2,2) > >> is.atomic(b) || is.list(b) > [1] TRUE > > > "It is common to call the atomic types ?atomic vectors?, but note that > is.vector imposes further restrictions: an object can be atomic but not a > vector (in that sense)." > > https://stat.ethz.ch/R-manual/R-devel/library/base/html/is.recursive.html > > I think a matrix is always atomic. So a matrix is "not a vector (in that > sense)," but "is.matrix returns TRUE if x is a vector and has a 'dim' > attribute of length 2." > > I do think I get what is going on with this, but why should I buy into > this conceptualization? Why is it better to say that a matrix *is* a > vector than to say that a matrix *contains* a vector? The latter seems to > be the more common way of thinking but such things."More common"? The better way to think of this is as a class hierarchy. A matrix is a particular kind of vector (the kind that has a dimension attribute). A matrix has all the properties that a vector has, plus some more. Would you say a cube contains a polygon, or a cube is a polygon? Duncan Murdoch Even in R you've had> to construct two different definitions of "vector" to deal with the > inconsistency created by the "matrix is a vector" way of thinking. So > there must be something really good about it that I am not understanding > (and I'm not being facetious or ironic!) > > Mike > ______________________________________________ > 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. >
Gabor Grothendieck
2014-Dec-25 21:26 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, Dec 25, 2014 at 1:57 PM, Mike Miller <mbmiller+l at gmail.com> wrote:> I do think I get what is going on with this, but why should I buy into this > conceptualization? Why is it better to say that a matrix *is* a vector than > to say that a matrix *contains* a vector? The latter seems to be the more > common way of thinking but such things. Even in R you've had to construct > two different definitions of "vector" to deal with the inconsistency created > by the "matrix is a vector" way of thinking. So there must be something > really good about it that I am not understanding (and I'm not being > facetious or ironic!)I think its the idea that in R all data objects are vectors (for some notion of vector) in the sense that all Lisp objects are lists, all APL objects are arrays and all tcl objects are character strings. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Mike Miller
2014-Dec-25 22:45 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Mike Miller wrote:> I was going to ask a question about it how to test that an object is a > vector, but then I found this: > > "is.vector() does not test if an object is a vector. Instead it returns > TRUE only if the object is a vector with no attributes apart from names. > Use is.atomic(x) || is.list(x) to test if an object is actually a > vector." > > From here: > > http://adv-r.had.co.nz/Data-structures.html#vectorsBut here... https://stat.ethz.ch/R-manual/R-devel/library/base/html/vector.html ...I read, "Note that factors are *not* vectors" (emphasis theirs), yet...> d <- gl(2,2)> is.factor(d)[1] TRUE> is.atomic(d) || is.list(d)[1] TRUE> is.list(d)[1] FALSE> is.atomic(d)[1] TRUE> is.vector(d)[1] FALSE So the factor is not a vector according to R documentation, but it is a vector according to the Wickham test, and it is not a vector according to is.vector(). Admittedly, the latter seems not to mean much to the R experts. Maybe a factor is just a vector with additional attributes. Mike