hello,everyone. I have one question: example 1> x=numeric(0) > y=5 > print(x+y)numeric(0) example 2> x=numeric(1) > y=5 > print(x+y)[1] 5 why the print(x+y) is numeric(0) at the first example, but the result is 0 at the second example? __________________________________________________ 佈伵伝仮伱佲伔佈G佊伿佅佷仯伃佒佇伖侜伒佢佉伝伨侙佄佫伬伂伝侙佊伿伡侢伾仹伻伵伋伂伌侒佊伿佅佷
yea, I have read the help. But some one tell me that i f you want use a vector that you don't know it's length, you should use xx=numric(0) , is it not right? If it isn't right, how can I do? thanks -----Original Message----- From: Douglas Grove [mailto:dgrove at fhcrc.org] Sent: Wednesday, June 01, 2005 6:27 AM To: luan_sheng Subject: Re: [R] FW: why is it numeric(0)? Have you read the help page for numeric (?numeric) to understand what it does? You should really look at help pages prior to posting. numeric(x) returns a numeric vector of length x, with all entries initialized to zero so numeric(1) returns 0, numeric(2) returns c(0,0) etc. numeric(0) returns a numeric vector of *length 0*, so when you add anything to it you get the same result (it's basically a numeric NULL) On Wed, 1 Jun 2005, luan_sheng wrote:> hello,everyone. I have one question: > > example 1 > > x=numeric(0) > > y=5 > > print(x+y) > numeric(0) > > example 2 > > x=numeric(1) > > y=5 > > print(x+y) > [1] 5 > > why the print(x+y) is numeric(0) at the first example, but the result > is 0 at the second example? > > > __________________________________________________ > > 佈伵伝仮伱佲伔佈G佊伿佅佷仯伃佒佇伖侜伒佢佉伝伨侙佄佫伬伂伝侙佊伿伡侢伾仹伻伵伋伂伌侒佊伿佅佷 > > ______________________________________________ > R-help at 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 >__________________________________________________ 佈伵伝仮伱佲伔佈G佊伿佅佷仯伃佒佇伖侜伒佢佉伝伨侙佄佫伬伂伝侙佊伿伡侢伾仹伻伵伋伂伌侒佊伿佅佷
?numeric Hint: Type numeric(0) and numeric(1) at the prompt and see what you get. What is the sum of anything and a zero length vector? -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of luan_sheng > Sent: Tuesday, May 31, 2005 3:22 PM > To: r-help at stat.math.ethz.ch > Subject: [R] FW: why is it numeric(0)? > > hello,everyone. I have one question: > > example 1 > > x=numeric(0) > > y=5 > > print(x+y) > numeric(0) > > example 2 > > x=numeric(1) > > y=5 > > print(x+y) > [1] 5 > > why the print(x+y) is numeric(0) at the first example, but > the result is 0 > at the second example? > > > __________________________________________________ > > QE;"Cb7QGSJOd#-VP9z5ZR;>xN^@,;xSJ<~I'HE3,4sSJOd > > ______________________________________________ > R-help at 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 >
On Wed, 1 Jun 2005, luan_sheng wrote:> hello,everyone. I have one question: > > example 1 >> x=numeric(0) >> y=5 >> print(x+y) > numeric(0) > > example 2 >> x=numeric(1) >> y=5 >> print(x+y) > [1] 5 > > why the print(x+y) is numeric(0) at the first example, but the result is 0 > at the second example? >numeric(0) is a zero-length vector of floating point numbers, so your first example takes no floating point numbers and adds 5 to each one. The result is still no floating point numbers. numeric(1) is a vector containing a single 0, so the second example takes 0 and adds 5, to give a vector containing a single 5. -thomas