The R Project SVN R

Rev

Rev 68948 | Rev 81930 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 69847
Line 1... Line 1...
1
% File src/library/base/man/numeric.Rd
1
% File src/library/base/man/numeric.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2014 R Core Team
3
% Copyright 1995-2016 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{numeric}
6
\name{numeric}
7
\title{Numeric Vectors}
7
\title{Numeric Vectors}
8
\alias{numeric}
8
\alias{numeric}
Line 55... Line 55...
55
  (\link{type} \code{"double"} or type \code{"integer"}) and not a
55
  (\link{type} \code{"double"} or type \code{"integer"}) and not a
56
  factor, and \code{FALSE} otherwise.  That is,
56
  factor, and \code{FALSE} otherwise.  That is,
57
  \code{is.integer(x) || is.double(x)}, or
57
  \code{is.integer(x) || is.double(x)}, or
58
  \code{(mode(x) == "numeric") && !is.factor(x)}.
58
  \code{(mode(x) == "numeric") && !is.factor(x)}.
59
}
59
}
-
 
60
\section{Warning}{
-
 
61
  If \code{x} is a \code{\link{factor}}, \code{as.numeric} will return
-
 
62
  the underlying numeric (integer) representation, which is often
-
 
63
  meaningless as it may not correspond to the \code{factor}
-
 
64
  \code{\link{levels}}, see the \sQuote{Warning} section in
-
 
65
  \code{\link{factor}} (and the 2nd example below).
-
 
66
}
60
\section{S4 methods}{
67
\section{S4 methods}{
61
  \code{as.numeric} and \code{is.numeric} are internally S4 generic and
68
  \code{as.numeric} and \code{is.numeric} are internally S4 generic and
62
  so methods can be set for them \emph{via} \code{setMethod}.
69
  so methods can be set for them \emph{via} \code{setMethod}.
63
 
70
 
64
  To ensure that \code{as.numeric} and \code{as.double}
71
  To ensure that \code{as.numeric} and \code{as.double}
Line 88... Line 95...
88
  \emph{The New S Language}.
95
  \emph{The New S Language}.
89
  Wadsworth & Brooks/Cole.
96
  Wadsworth & Brooks/Cole.
90
}
97
}
91
\examples{
98
\examples{
92
as.numeric(c("-.1"," 2.7 ","B")) # (-0.1, 2.7, NA)  +  warning
99
as.numeric(c("-.1"," 2.7 ","B")) # (-0.1, 2.7, NA)  +  warning
-
 
100
 
-
 
101
as.numeric(factor(5:10)) # not what you'd expect
-
 
102
f <- factor(1:5)
-
 
103
## what you typically meant and want:
93
as.numeric(factor(5:10))
104
as.numeric(as.character(f))
-
 
105
## the same, considerably (for long factors) more efficient:
-
 
106
as.numeric(levels(f))[f]
94
}
107
}
95
\keyword{classes}
108
\keyword{classes}
96
\keyword{attribute}
109
\keyword{attribute}