# why is 0 not an integer?

12 messages
Open this post in threaded view
|

## 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[1] <- 0 > class(v) [1] "numeric"  #!! >
Open this post in threaded view
|

## Re: why is 0 not an integer?

 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"  #!! >> > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: why is 0 not an integer?

 In reply to this post by sjaffe 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] <- 0 try this: v <- as.integer(0) Nikos > > class(v) > [1] "numeric"  #!! > > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: why is 0 not an integer?

 In reply to this post by sjaffe 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: [hidden email] [mailto:[hidden email]] On Behalf Of Steve Jaffe Sent: Wednesday, August 05, 2009 3:16 PM To: [hidden email] 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.htmlSent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: why is 0 not an integer?

 In reply to this post by sjaffe 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 <[hidden email]> wrote: > From: Steve Jaffe <[hidden email]> > Subject: [R]  why is 0 not an integer? > To: [hidden email] > 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. > > ______________________________________________ > [hidden email] > 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: why is 0 not an integer?

 In reply to this post by sjaffe 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<[hidden email]> 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. > > ______________________________________________ > [hidden email] 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? ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: why is 0 not an integer?

 In reply to this post by Erik Iverson-2 On Wed, Aug 5, 2009 at 11:28 PM, Erik Iverson <[hidden email]> wrote: > 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. According to R Language Definition, foo[0] always returns an empty vector. No idea why this is useful but it is a bit confusing that one can actually assign to foo[0] (and even foo[NULL] ) with some effect: foo <- 1:10 class(foo)  # integer foo[NULL] <- "une souris verte" foo     # all the same numbers there class(foo)  # changed to character!! k > > 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: [hidden email] [mailto:[hidden email]] > On Behalf Of Steve Jaffe > Sent: Wednesday, August 05, 2009 3:16 PM > To: [hidden email] > 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. > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Why is 0 not an integer?

 In reply to this post by sjaffe 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 [hidden email] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Why is 0 not an integer?

 By the way: Are there difference between -0 and 0? On Thu, Aug 6, 2009 at 5:00 PM, Gerlanc, Daniel < [hidden email]> wrote: > 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 > [hidden email] > > ______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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, Giovanni > By the way: > > Are there difference between -0 and 0? ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.