[R] ave(x, y, FUN=length) produces character output when x is character
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Dec 25 21:41:32 CET 2014
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.
>
More information about the R-help
mailing list