[R] ave(x, y, FUN=length) produces character output when x is character

Mike Miller mbmiller+l at gmail.com
Thu Dec 25 19:57:05 CET 2014

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:


> a <- c(1,2,3,4)

> names(a) <- LETTERS[1:4]

> attr(a, "vecinfo") <- "yes, I'm a vector"

> a
1 2 3 4
[1] "yes, I'm a vector"

> attributes(a)
[1] "A" "B" "C" "D"

[1] "yes, I'm a vector"

> is.vector(a)

> 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)."


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!)


More information about the R-help mailing list