data.matrix
Coerce Data Frame to Numeric Matrix
Description
This function takes a data frame as an argument and returns a numeric matrix
whose elements correspond to the elements of the data frame.
Usage
data.matrix(frame, rownames.force = NA)
Arguments
frame |
a data frame. |
rownames.force |
a logical value indicating if the row names are forced.
- If TRUE, the rownames attribute of frame
is forced to be used as the row names of the returned matrix.
- If FALSE, the rownames attribute is always NULL.
- If NA or any other value, and when the length of rownames of frame is 0,
the rownames attribute of the matrix is NULL.
Otherwise, the row names of frame are used as the row names of the matrix.
|
Details
Integer and other numeric variables are kept with no change.
Factors and logical variables are converted to integer using as.integer.
Other non-numeric variables are converted to numeric using as.numeric.
The result is an integer or numeric data matrix (can contain NAs).
Value
returns a matrix corresponding to the data frame.
The mode and dimensions of the matrix depend on the variables in the data frame.
Differences between TIBCO Enterprise Runtime for R and Open-source R
The function numerical.matrix is not implemented in open-source R.
References
Chambers, J. M. (1992)
Data for models.
Chapter 3 of Statistical Models in S
eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
See Also
Examples
# fuel.frame is a dataset in the package Sdatasets.
data(fuel.frame, package = "Sdatasets")
as.matrix(fuel.frame) # produces a character matrix
# Weight Disp. Mileage Fuel Type
# Eagle Summit 4 "2560" " 97" "33" "3.030303" "Small"
# Ford Escort 4 "2345" "114" "33" "3.030303" "Small"
# ...
data.matrix(fuel.frame) # factors converted to codes
# Weight Disp. Mileage Fuel Type
# Eagle Summit 4 2560 97 33 3.030303 4
# Ford Escort 4 2345 114 33 3.030303 4
# ... (contains an attribute "column.levels")
# y_frame contains date-time class variables.
x_date <- seq(as.POSIXlt("2010-1-1"), as.POSIXlt("2010-5-1"),
length.out = 5)
y_frame <- data.frame(a = x_date, b = c(1:4, NA), c =letters[5:1],
d = c(NA, TRUE, FALSE, NA,FALSE))
as.matrix(y_frame)
data.matrix(y_frame)
# Further examples
x <- data.frame(1:3, letters[1:3])
as.matrix(x); data.matrix(x)
x <- data.frame(1:3, letters[1:3], stringsAsFactors = F)
as.matrix(x); data.matrix(x)