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

Bert Gunter gunter.berton at gene.com
Thu Dec 25 06:27:01 CET 2014


You are again misinterpreting because you have not read the docs,
although this time I will grant that they are to some extent
misleading.

First of all, a matrix _IS_ a vector:

> a <- matrix(1:4, 2,2)
> a[3] ## vector indexing works because it is a vector
[1] 3

In fact, a matrix (or array) is a vector with a "dim" attribute. This
is documented in ?matrix:

"is.matrix returns TRUE if x is a vector and has a "dim" attribute of
length 2) and FALSE otherwise."

Your confusion arises because, despite its name, is.vector() does not
actually test whether something "is" a vector (after all these are all
abstractions; what it "is" is contents of memory, implemented as a
linked list or some such).  ?is.vector tells you:

"is.vector returns TRUE if x is a vector of the specified mode having
no attributes other than names. It returns FALSE otherwise."

An array has a "dim" attribute, so is.vector() returns FALSE on it.
But it actually _is_ ("behaves like") a vector (in column major
order,actually).

Now you may complain that this is confusing and I would agree. Why is
it this way? I dunno -- probably due to historical quirks -- evolution
is not necessarily orderly. But that's the way it is; that's the way
it's documented; and tutorials will tell you about this (that's how I
learned). So please stop guessing and intuiting and read the docs to
understand how things work.

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




On Wed, Dec 24, 2014 at 8:45 PM, Mike Miller <mbmiller+l at gmail.com> wrote:
> On Wed, 24 Dec 2014, Jeff Newmiller wrote:
>
>> On December 24, 2014 6:49:47 PM PST, Mike Miller <mbmiller+l at gmail.com>
>> wrote:
>>
>>> On Wed, 24 Dec 2014, Mike Miller wrote:
>>>
>>>> Also, regarding the sacred text, "x A numeric." is a bit terse.  The
>>>> same text later refers to length(x), so I suspect that "A numeric" is
>>>
>>>
>>>> short for "A numeric vector", but that might not mean "a vector of
>>>> 'numeric' type."
>>>
>>>
>>>
>>> I just realized that numeric type includes integer so that anything of
>>> type integer also is type numeric.  I'm working on another message.
>>
>>
>>
>> But all numeric types in R are vectors. So although it might be a good
>> idea to be redundant to aid beginners, the phrase "a numeric" is accurate.
>
>
>
> Interesting, but the data seem to contradict your theory.  Here are two
> examples, one with a numeric matrix, the other with a numeric array, and
> both of them run in ave().
>
>> x <- matrix(1:4, 2,2)
>
>
>> is.numeric(x)
>
> [1] TRUE
>
>> is.vector(x)
>
> [1] FALSE
>
>> ave(x, gl(2,2))
>
>      [,1] [,2]
> [1,]  1.5  3.5
> [2,]  1.5  3.5
>
>
>> x <- as.array(1:4)
>
>
>> is.numeric(x)
>
> [1] TRUE
>
>> is.vector(x)
>
> [1] FALSE
>
>> ave(x, gl(2,2))
>
> [1] 1.5 1.5 3.5 3.5
>
>
> So maybe slightly more documentation for ave() would be helpful even for
> non-beginners like yourself.
>
> Thanks.
>
> Mike
>
> --
> Michael B. Miller, Ph.D.
> University of Minnesota
> http://scholar.google.com/citations?user=EV_phq4AAAAJ



More information about the R-help mailing list