1

I don't seem to be able to produce a vector of integers using structure().

> x <- structure(c(1,2), class='integer', mode='integer', storage.mode='integer')
> paste(class(x), mode(x), storage.mode(x), is.integer(x))
[1] "integer numeric double FALSE"

Compare with a true vector of integers:

> y <- as.integer(c(1,2))
> paste(class(y), mode(y), storage.mode(y), is.integer(y))
[1] "integer numeric integer TRUE"

Any idea?

Ernest A
  • 7,526
  • 8
  • 34
  • 40
  • 1
    Arguments to `structure` set `attributes`, which is not how `typeof` determines the `storage.mode`. `class` is determined by an attribute, unless missing where it is given an implicit class. – James Apr 12 '12 at 13:59
  • 1
    I agree that this is an inconsistency in R, as the documentation for `structure`, `as.integer` , `typeof` don't really suggest the conflict presented here. `attributes` and `mode` of an object are different. – Carl Witthoft Apr 12 '12 at 15:03
  • 1
    See http://stackoverflow.com/q/7014387/662787 for more info on `1` vs `1L`. – Tommy Apr 12 '12 at 16:17
  • 1
    ...and http://stackoverflow.com/a/8857411/662787 for more on class/mode/storage.mode etc. – Tommy Apr 12 '12 at 16:19

1 Answers1

2

try this:

> x <- structure(c(1L,2L))
> is.integer(x)
[1] TRUE

but just

> x <- c(1L,2L)
> is.integer(x)
[1] TRUE

is ok.

kohske
  • 65,572
  • 8
  • 165
  • 155