The R Project SVN R

Rev

Rev 88581 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/integer.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
59039 ripley 3
% Copyright 1995-2010 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{integer}
56186 murdoch 7
\alias{integer}
880 hornik 8
\alias{as.integer}
9
\alias{is.integer}
7247 hornik 10
\title{Integer Vectors}
11
\description{
12
  Creates or tests for objects of type \code{"integer"}.
13
}
2 r 14
\usage{
880 hornik 15
integer(length = 0)
10525 ripley 16
as.integer(x, \dots)
2 r 17
is.integer(x)
18
}
15518 ripley 19
\arguments{
85065 smeyer 20
  \item{length}{a non-negative integer specifying the desired length.
56158 ripley 21
    Double values will be coerced to integer:
56156 ripley 22
    supplying an argument of length other than one is an error.}
15518 ripley 23
  \item{x}{object to be coerced or tested.}
24
  \item{\dots}{further arguments passed to or from other methods.}
25
}
30835 ripley 26
\details{
27
  Integer vectors exist so that data can be passed to C or Fortran code
51132 ripley 28
  which expects them, and so that (small) integer data can be represented
30835 ripley 29
  exactly and compactly.
30
 
51132 ripley 31
  Note that current implementations of \R use 32-bit integers for
32
  integer vectors, so the range of representable integers is restricted
33
  to about \eqn{\pm 2 \times 10^9}{+/-2*10^9}: \code{\link{double}}s can
34
  hold much larger integers exactly.
30835 ripley 35
}
2 r 36
\value{
7247 hornik 37
  \code{integer} creates a integer vector of the specified length.
880 hornik 38
  Each element of the vector is equal to \code{0}.
2 r 39
 
880 hornik 40
  \code{as.integer} attempts to coerce its argument to be of integer
55555 ripley 41
  type.  The answer will be \code{NA} unless the coercion succeeds.  Real
51194 ripley 42
  values larger in modulus than the largest integer are coerced to
43
  \code{NA} (unlike S which gives the most extreme integer of the same
44
  sign).  Non-integral numeric values are truncated towards zero (i.e.,
22694 ripley 45
  \code{as.integer(x)} equals \code{\link{trunc}(x)} there), and
46
  imaginary parts of complex numbers are discarded (with a warning).
51194 ripley 47
  Character strings containing optional whitespace followed by either a
48
  decimal representation or a hexadecimal representation (starting with
90049 hornik 49
  \samp{0x} or \samp{0X}) can be converted, as well as any allowed by
51194 ripley 50
  the platform for real numbers.  Like \code{\link{as.vector}} it strips
51
  attributes including names.  (To ensure that an object \code{x} is of
52
  integer type without stripping attributes, use
53
  \code{\link{storage.mode}(x) <- "integer"}.)
2 r 54
 
880 hornik 55
  \code{is.integer} returns \code{TRUE} or \code{FALSE} depending on
40667 ripley 56
  whether its argument is of integer \link{type} or not, unless it is a
57
  factor when it returns \code{FALSE}.
2 r 58
}
50593 ripley 59
\note{
60
  \code{is.integer(x)} does \bold{not} test if \code{x} contains integer
61
  numbers!  For that, use \code{\link{round}}, as in the function
62
  \code{is.wholenumber(x)} in the examples.
63
}
24300 ripley 64
\references{
88581 hornik 65
  \bibshow{R:Becker+Chambers+Wilks:1988}
24300 ripley 66
}
22215 ripley 67
\seealso{
40923 ripley 68
  \code{\link{numeric}}, \code{\link{storage.mode}}.
69
 
22215 ripley 70
  \code{\link{round}} (and \code{ceiling} and \code{floor} on that help
71
  page) to convert to integral values.
72
}
22694 ripley 73
\examples{
48377 maechler 74
## as.integer() truncates:
61168 ripley 75
x <- pi * c(-1:1, 10)
48377 maechler 76
as.integer(x)
77
 
78
is.integer(1) # is FALSE !
79
 
50593 ripley 80
is.wholenumber <-
81
    function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol
48377 maechler 82
is.wholenumber(1) # is TRUE
61150 ripley 83
(x <- seq(1, 5, by = 0.5) )
48377 maechler 84
is.wholenumber( x ) #-->  TRUE FALSE TRUE ...
22694 ripley 85
}
286 maechler 86
\keyword{classes}