The R Project SVN R

Rev

Rev 59039 | Rev 76937 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/data.matrix.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2008 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{data.matrix}
56186 murdoch 7
\alias{data.matrix}
40320 ripley 8
\title{Convert a Data Frame to a Numeric Matrix}
7782 hornik 9
\description{
10
  Return the matrix obtained by converting all the variables in a data
11
  frame to numeric mode and then binding them together as the columns of
24209 ripley 12
  a matrix.  Factors and ordered factors are replaced by their internal
13
  codes.
7782 hornik 14
}
2 r 15
\usage{
40320 ripley 16
data.matrix(frame, rownames.force = NA)
2 r 17
}
18
\arguments{
20507 ripley 19
  \item{frame}{a data frame whose components are logical vectors,
7782 hornik 20
    factors or numeric vectors.}
40331 ripley 21
  \item{rownames.force}{logical indicating if the resulting matrix
22
    should have character (rather than \code{NULL})
23
    \code{\link{rownames}}.  The default, \code{NA}, uses \code{NULL}
24
    rownames if the data frame has \sQuote{automatic} row.names or for a
25
    zero-row data frame.}
2 r 26
}
32212 ripley 27
\details{
46206 ripley 28
  Logical and factor columns are converted to integers.  Any other
29
  column which is not numeric (according to \code{\link{is.numeric}}) is
30
  converted by \code{\link{as.numeric}} or, for S4 objects,
31
  \code{\link{as}(, "numeric")}.  If all columns are integer (after
48869 murdoch 32
  conversion) the result is an integer matrix, otherwise a numeric
46206 ripley 33
  (double) matrix.
32212 ripley 34
}
37122 ripley 35
\value{
40211 ripley 36
  If \code{frame} inherits from class \code{"data.frame"}, an integer or
37
  numeric matrix of the same dimensions as \code{frame}, with dimnames
40320 ripley 38
  taken from the \code{row.names} (or \code{NULL}, depending on
39
  \code{rownames.force}) and \code{names}.
37122 ripley 40
 
41
  Otherwise, the result of \code{\link{as.matrix}}.
42
}
40320 ripley 43
\note{
40331 ripley 44
  The default behaviour for data frames differs from \R < 2.5.0 which
40320 ripley 45
  always gave the result character rownames.
46
}
2 r 47
\seealso{
7782 hornik 48
  \code{\link{as.matrix}},
49
  \code{\link{data.frame}},
50
  \code{\link{matrix}}.
2 r 51
}
24239 ripley 52
\references{
53
  Chambers, J. M. (1992)
54
  \emph{Data for models.}
55
  Chapter 3 of \emph{Statistical Models in S}
47262 ripley 56
  eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
24239 ripley 57
}
32212 ripley 58
\examples{
61150 ripley 59
DF <- data.frame(a = 1:3, b = letters[10:12],
60
                 c = seq(as.Date("2004-01-01"), by = "week", len = 3),
38319 ripley 61
                 stringsAsFactors = TRUE)
32212 ripley 62
data.matrix(DF[1:2])
46206 ripley 63
data.matrix(DF)
32212 ripley 64
}
286 maechler 65
\keyword{array}