| numeric | R Documentation |
Creates or coerces objects of type"numeric".is.numeric is a more general test of an object beinginterpretable as numbers.
numeric(length = 0)as.numeric(x, ...)is.numeric(x)
length | A non-negative integer specifying the desired length.Double values will be coerced to integer:supplying an argument of length other than one is an error. |
x | object to be coerced or tested. |
... | further arguments passed to or from other methods. |
numeric is identical todouble (andreal).It creates a double-precision vector of the specified length with eachelement equal to0.
as.numeric is a generic function, but S3 methods must bewritten foras.double. It is identical toas.double.
is.numeric is an internal genericprimitivefunction: you can write methods to handle specific classes of objects,see InternalMethods. It isnot the same asis.double. Factors are handled by the default method,and there are methods for classes"Date","POSIXt" and"difftime" (all of whichreturn false). Methods foris.numeric should only return trueif the base type of the class isdouble orintegerand values can reasonably be regarded as numeric(e.g., arithmetic on them makes sense, and comparison should be donevia the base type).
fornumeric andas.numeric seedouble.
The default method foris.numeric returnsTRUEif its argument is of mode"numeric"(type"double" or type"integer") and not afactor, andFALSE otherwise. That is,is.integer(x) || is.double(x), or(mode(x) == "numeric") && !is.factor(x).
Ifx is afactor,as.numeric will returnthe underlying numeric (integer) representation, which is oftenmeaningless as it may not correspond to thefactorlevels, see the ‘Warning’ section infactor (and the 2nd example below).
as.numeric andis.numeric are internally S4 generic andso methods can be set for themviasetMethod.
To ensure thatas.numeric andas.doubleremain identical, S4 methods can only be set foras.numeric.
It is a historical anomaly thatR has two names for itsfloating-point vectors,double andnumeric(and formerly hadreal).
double is the name of the type.numeric is the name of the mode and also of the implicitclass. As an S4 formal class, use"numeric".
The potential confusion is thatR has usedmode"numeric" to mean ‘double or integer’, which conflictswith the S4 usage. Thusis.numeric tests the mode, not theclass, butas.numeric (which is identical toas.double)coerces to the class.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
double,integer,storage.mode.
## Conversion does trim whitespace; non-numeric strings give NA + warningas.numeric(c("-.1"," 2.7 ","B"))## Numeric values are sometimes accidentally converted to factors.## Converting them back to numeric is trickier than you'd expect.f <- factor(5:10)as.numeric(f) # not what you might expect, probably not what you want## what you typically meant and want:as.numeric(as.character(f))## the same, considerably more efficient (for long vectors):as.numeric(levels(f))[f]Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.