Skip to content
Back to formatted view

Raw Message

Message-ID: <20050413021138.60353.qmail@web31310.mail.mud.yahoo.com>
Date: 2005-04-13T02:11:37Z
From: Vivek Rao
Subject: Why is 1 a double?

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