[R] Why is 1 a double?

Vivek Rao rvivekrao at yahoo.com
Wed Apr 13 04:11:37 CEST 2005


Based on examples in R books and the syntax of other
programming languages, I expected that

n <- 10

assigns the integer 10 to n, but typeof(n) is actually
a double. The subscripting expression x[1] is valid,
but sprintf("\n %d",1) is not, giving the error

Error in sprintf("\n %d", 1) : use format %f, %e or %g
for numeric objects

One must use instead sprintf("\n %d",as.integer(1)). 

Two questions:

(1) When one intends to define an integer variable,
should a construction such as

n <- as.integer(10)

be used? Most examples I have seen don't do this. 

(2) Why wasn't the S language defined so that 
typeof(1) is "integer" rather than "double"?




More information about the R-help mailing list