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. >
Bert Gunter
2014-Dec-25 20:59 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, Dec 25, 2014 at 12:41 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> Would you say a cube contains a polygon, or a cube is a polygon?Neither, actually. I'd say a cube is a polyhedron or a square is a polygon. :-) But point taken, of course. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll
Mike Miller
2014-Dec-25 21:19 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On Thu, 25 Dec 2014, Duncan Murdoch wrote:> On 25/12/2014 1:57 PM, Mike Miller 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. > > "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?I would say that the sides of the cube are polygons, so I guess a cube "contains" six polygons, but "is" would be wrong because a cube is a polyhedron, not a polygon. A cube is not a polygon. I get your point about hierarchy. Someone else showed me that setting the dim attribute to NULL changes the matrix into a vector so that is.vector() is TRUE. Mike
Duncan Murdoch
2014-Dec-25 21:25 UTC
[R] ave(x, y, FUN=length) produces character output when x is character
On 25/12/2014 3:59 PM, Bert Gunter wrote:> On Thu, Dec 25, 2014 at 12:41 PM, Duncan Murdoch > <murdoch.duncan at gmail.com> wrote: >> Would you say a cube contains a polygon, or a cube is a polygon? > > Neither, actually. I'd say a cube is a polyhedron or a square is a polygon. > > :-) > > But point taken, of course.I have trouble remembering how many dimensions I live in. Duncan