The R Project SVN R

Rev

Rev 80079 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
76937 hornik 3
% Copyright 1995-2019 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,
76937 hornik 20
    factors or numeric or character 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{
76937 hornik 28
  Logical and factor columns are converted to integers.  Character
29
  columns are first converted to factors and then to integers. Any other
46206 ripley 30
  column which is not numeric (according to \code{\link{is.numeric}}) is
31
  converted by \code{\link{as.numeric}} or, for S4 objects,
32
  \code{\link{as}(, "numeric")}.  If all columns are integer (after
48869 murdoch 33
  conversion) the result is an integer matrix, otherwise a numeric
46206 ripley 34
  (double) matrix.
32212 ripley 35
}
37122 ripley 36
\value{
40211 ripley 37
  If \code{frame} inherits from class \code{"data.frame"}, an integer or
38
  numeric matrix of the same dimensions as \code{frame}, with dimnames
40320 ripley 39
  taken from the \code{row.names} (or \code{NULL}, depending on
40
  \code{rownames.force}) and \code{names}.
37122 ripley 41
 
42
  Otherwise, the result of \code{\link{as.matrix}}.
43
}
40320 ripley 44
\note{
40331 ripley 45
  The default behaviour for data frames differs from \R < 2.5.0 which
40320 ripley 46
  always gave the result character rownames.
47
}
2 r 48
\seealso{
7782 hornik 49
  \code{\link{as.matrix}},
50
  \code{\link{data.frame}},
51
  \code{\link{matrix}}.
2 r 52
}
24239 ripley 53
\references{
88585 hornik 54
  \bibshow{R:Chambers:1992:c3}
24239 ripley 55
}
32212 ripley 56
\examples{
61150 ripley 57
DF <- data.frame(a = 1:3, b = letters[10:12],
80079 hornik 58
                 c = seq(as.Date("2004-01-01"), by = "week", length.out = 3),
38319 ripley 59
                 stringsAsFactors = TRUE)
32212 ripley 60
data.matrix(DF[1:2])
46206 ripley 61
data.matrix(DF)
32212 ripley 62
}
286 maechler 63
\keyword{array}