Why when I assign 0 to an element of an integer vector does the type change to numeric? 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" #!!>-- View this message in context: http://www.nabble.com/why-is-0-not-an-integer--tp24835423p24835423.html Sent from the R help mailing list archive at Nabble.com.
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" #!! >> >
On Wed, 2009-08-05 at 13:16 -0700, Steve Jaffe wrote:> Why when I assign 0 to an element of an integer vector does the type change > to numeric? > 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] <- 0try this: v <- as.integer(0) Nikos> > class(v) > [1] "numeric" #!! > > >
First, this has nothing to do with "0". Assigning 1000 to an element of v would also have this effect. Two, the first element of a vector is indexed by "1", not "0". While what you wrote isn't a syntax error (v[0] <- 0), it may be not doing what you think, but I don't know. Finally, the answer to your question. Try typing class(0) to see that it is in fact numeric. So you may want v[1] <- as.integer(0) to get what you are expecting. HTH. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Jaffe Sent: Wednesday, August 05, 2009 3:16 PM To: r-help at r-project.org Subject: [R] why is 0 not an integer? Why when I assign 0 to an element of an integer vector does the type change to numeric? 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[0] <- 0 > class(v)[1] "numeric" #!!>-- View this message in context: http://www.nabble.com/why-is-0-not-an-integer--tp24835423p24835423.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Pesumably because v[1] <- 0 give a numeric result, then the rest of v is coerced into numeric. Observe v <- 0:10 class(v) v[1] <- as.integer(0) class(v[1]) class(v) --- On Wed, 8/5/09, Steve Jaffe <sjaffe at riskspan.com> wrote:> From: Steve Jaffe <sjaffe at riskspan.com> > Subject: [R] why is 0 not an integer? > To: r-help at r-project.org > Received: Wednesday, August 5, 2009, 4:16 PM > > Why when I assign 0 to an element of an integer vector does > the type change > to numeric? > 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"? #!! > > > > -- > View this message in context: http://www.nabble.com/why-is-0-not-an-integer--tp24835423p24835423.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org > mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >__________________________________________________________________ The new Internet Explorer? 8 - Faster, safer, easier. Optimized for Yahoo! Get it Now for Fr
try> x <- 0 > class(x)[1] "numeric"> x <- 0L > class(x)[1] "integer">You have to explicitly indicate that you want integer. On Wed, Aug 5, 2009 at 4:16 PM, Steve Jaffe<sjaffe at riskspan.com> wrote:> > Why when I assign 0 to an element of an integer vector does the type change > to numeric? > 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" ?#!! >> > > -- > View this message in context: http://www.nabble.com/why-is-0-not-an-integer--tp24835423p24835423.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
b/c> class(0)[1] "numeric"> typeof(0)[1] "double"> class(0L)[1] "integer"> typeof(0L)[1] "integer" When you call the ":" function it always returns an integer sequence, but when you assign a numeric to an element of the vector it gets coerced to the more general type, in this case, numeric. Note that "is.numeric" returns TRUE for both is.numeric(0) and is.numeric(0L). Daniel Gerlanc Associate Analyst Geode Capital Management 1 Post Office Sq, Floor 28 Boston, MA 02109 Daniel.Gerlanc at geodecapital.com
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Giovanni Petris > Sent: Thursday, August 06, 2009 3:00 PM > To: milton.ruser at gmail.com > Cc: r-help at r-project.org; Daniel.Gerlanc at geodecapital.com > Subject: Re: [R] Why is 0 not an integer? > > > I ran an instant experiment... > > > typeof(0) > [1] "double" > > typeof(-0) > [1] "double" > > identical(0, -0) > [1] TRUE > > Best, > GiovanniBut 0.0 and -0.0 have different reciprocals > 1.0/0.0 [1] Inf > 1.0/-0.0 [1] -Inf Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> > > By the way: > > > > Are there difference between -0 and 0? > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >