[Rd] as(1:4, "numeric") versus as.numeric(1:4, "numeric")

Hervé Pagès hpages at fhcrc.org
Thu Apr 1 00:52:11 CEST 2010


Hi,

   > class(as(1:4, "numeric"))
   [1] "integer"

Surprising but an explanation could be that an integer
vector being a particular case of numeric vector, this
coercion has nothing to do because 1:4 is already numeric.
And indeed:

   > is.numeric(1:4)
   [1] TRUE
   > is.numeric(as(1:4, "numeric"))
   [1] TRUE

However, 'as(1:4, "numeric")' is inconsistent with

   > class(as.numeric(1:4))
   [1] "numeric"

And, even more confusing, if you look at the coerce,ANY,numeric
method:

   > selectMethod("coerce", c("integer", "numeric"))
   Method Definition:

   function (from, to, strict = TRUE)
   {
     value <- as.numeric(from)
     if (strict)
         attributes(value) <- NULL
     value
   }
   <environment: namespace:methods>

   Signatures:
           from      to
   target  "integer" "numeric"
   defined "ANY"     "numeric"

it calls as.numeric()!

So how can 'as(1:4, "numeric")' not return the same thing as
'as.numeric(1:4)' looks like a mystery to me. Could it be
conceivable that I found a bug?

Cheers,
H.


 > sessionInfo()
R version 2.11.0 Under development (unstable) (2010-03-15 r51282)
x86_64-unknown-linux-gnu

locale:
  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
  [5] LC_MONETARY=C              LC_MESSAGES=en_CA.UTF-8
  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base


-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list