3

It's probably gonna be a stupid question, but I can't find the answer quickly and I'm too curious to give up even if late.

In essence, why 1L:3L and letters are both vectors

> is.vector(1:3)
[1] TRUE
> is.vector(letters)
[1] TRUE

and a sequence of dates is not?

x <- structure(1:3, class = "Date")
> is.vector(x)
[1] FALSE

From ?vector

The atomic modes are "logical", "integer", "numeric" (synonym "double"),
"complex", "character" and "raw".

Fine, clear, even though x is atomic...

> is.atomic(x)
[1] TRUE

So, what makes a dates vector not to be read as vector? (in means of as.vector()) and what is there behind this difference?

This question comes from a try to use embed with dates which fails cause it wants vector or array. But from a structural point of view I can't see the difference between 1L:10L and structure(1L:10L, class="Date")

Michele
  • 8,563
  • 6
  • 45
  • 72
  • 5
    From `?is.vector`: "is.vector returns TRUE if x is a vector of the specified mode having no attributes _other than names_. It returns FALSE otherwise.". – joran Sep 16 '13 at 17:27
  • (i.e. `class` is an attribute.) – joran Sep 16 '13 at 17:28
  • @joran I see, thanks. but it makes no much sense anyway. e.g. to use `embed` I have to convert to number and then back to Date again... it's pointless! What I'm actually asking is why R distinguish between vector of number, char, logical and dates. – Michele Sep 16 '13 at 17:32
  • possible duplicate of [Looping over a Date object result in a numeric iterator](http://stackoverflow.com/questions/6434663/looping-over-a-date-object-result-in-a-numeric-iterator) - in particular, see [this answer](http://stackoverflow.com/a/7143086/) – Blue Magister Sep 16 '13 at 17:58
  • R thinks they're nice and punctual but doesn't really want to get into a serious relationship with them. (probably because R still have feelings for a vector of random integers...wild but structured without any of those messy decimals) – John Sep 16 '13 at 18:02
  • 2
    I think your real question then is why does `embed` use `is.vector` rather than, say, `is.atomic`. I'm not sure except to note that the behavior on lists would require some slight tweaking, and inertia. There might be other edge cases that I'm not aware of. – joran Sep 16 '13 at 18:10
  • A message to the R people: Things from which you notice that your naming conventions suck: 1) A vector of dates is not a vector (??) 2) a vector of a non atomic data type (Date) is atomic (??)... – Fabian Werner Sep 03 '15 at 07:55

1 Answers1

3

A few of my comments collected together in an answer:

So, the documentation also says:

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

So as discussed in this pervious answer, is.vector is more like a check on whether a vector has attributes other than names, which certainly may not be obvious.

As for why embed behaves the way it does, I'm not sure. It could potentially use is.atomic instead, but you'd then have to check for lists separately to achieve the same behavior. There may be some other edge cases I'm overlooking.

Community
  • 1
  • 1
joran
  • 169,992
  • 32
  • 429
  • 468