[R] Why is 1 a double?

From: Vivek Rao <rvivekrao_at_yahoo.com>
Date: Wed 13 Apr 2005 - 12:11:37 EST


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"?



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Apr 13 12:18:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST