Coerce Data Frame to Matrix

DESCRIPTION:

These function take a data frame as an argument and return a matrix whose elements correspond to the elements of the data frame.

USAGE:

as.matrix.data.frame(x, justify.format = "decimal", padStrings = F)
numerical.matrix(x, remove = F)
data.matrix(frame)

REQUIRED ARGUMENTS:

x
data frame.
frame
data frame.

OPTIONAL ARGUMENTS:

justify.format
string passed to format indicating what justification method to use when converting numerics to characters. Default is "decimal", use "none" to prevent justification.
padStrings
if TRUE all strings in a given column are padded to ensure equal widths.
remove
logical flag, if TRUE then non-numerical variables are removed.

VALUE:

a matrix corresponding to the data frame. The mode and dimensions of the matrix depend on the variables in the data frame.

DETAILS:

We first describe the behavior of as.matrix.data.frame , then note how the other functions differ. There are four notable cases. First, if all variables are numeric vectors, the result is a numeric matrix. Second, if factor or character variables are present, the resulting matrix is of mode "character"; numeric variables are converted to character and factors are replaced by the corresponding level values ( NA's in factors are converted to the string "NA"). Third, if any variable is not atomic (e.g., of mode "list"), the resulting matrix is of mode "list". Fourth, if any variable is a matrix or data frame, it contributes as many columns as it has to the result (independent of the considerations concerning mode discussed above).
For numerical.matrix, factors, character vectors, and other non-numerical data are converted to numerical missing values ( NA's); or if remove=TRUE the variables are removed (resulting in a matrix with fewer columns). Lists cause an error (or are removed). The result is a numerical matrix.
For data.matrix, factors are first transformed to numeric values using codes() , If factors are present the matrix returned has an attribute called "column.levels", a list with an element for each column of the matrix. The elements of this list are either NULL or else contain the levels of the factors prior to conversion by codes(). These codes are in general not meaningful for numerical computations such as mean.
as.matrix.data.frame is a method for the generic function for class data.frame. It can be invoked by calling for an object x of the appropriate class, or directly by calling as.matrix.data.frame regardless of the class of the object.

SEE ALSO:

, , , , .

EXAMPLES:

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"
                   ...
numerical.matrix(fuel.frame)  # numerical matrix
                        Weight Disp. Mileage     Fuel Type
         Eagle Summit 4   2560    97      33 3.030303   NA
        Ford Escort   4   2345   114      33 3.030303   NA
                   ...
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")

# Further examples; define any of the following data frames:
x <- data.frame(1:3, I(list(1:2, "c", T)))
x <- data.frame(1:3, letters[1:3])
x <- data.frame(1:3, letters[1:3], stringsAsFactors = F)
# then compare the results of
as.matrix(x)
numerical.matrix(x)
numerical.matrix(x, remove=T)
data.matrix(x)