[R] why is 0 not an integer?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Aug 5 22:21:55 CEST 2009


On 8/5/2009 4:16 PM, Steve Jaffe wrote:
> Why when I assign 0 to an element of an integer vector does the type change
> to numeric?

Because 0 is a numeric constant, not an integer constant. R doesn't 
check the value, only the type:  it's just as if you assigned 3.14159 to 
that element as far as R is concerned.

If you want the integer constant use 0L.

Duncan Murdoch

> Here is a particularly perplexing example:
>> v <- 0:10
>> v
>  [1]  0  1  2  3  4  5  6  7  8  9 10
>> class(v)
> [1] "integer"
>> v[1] <- 0
>> class(v)
> [1] "numeric"  #!!
>> 
>




More information about the R-help mailing list