Skip to content

Commit

Permalink
Make data.matrix() convert character columns to factor then integer.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@76937 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
hornik committed Aug 8, 2019
1 parent ad97e0d commit d1d3863
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
the user level functions \code{.traceback()} and \code{traceback()},
fulfilling the wish of \PR{17580}, reported (incl. patch proposal)
by Brodie Gaslam.
\item \code{data.matrix()} now converts character columns to
factors and from this to integers.
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/library/base/R/data.matrix.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# File src/library/base/R/data.matrix.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2012 The R Core Team
# Copyright (C) 1995-2019 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -26,9 +26,6 @@ data.matrix <- function(frame, rownames.force = NA)
else if(.row_names_info(frame) <= 0L) NULL
else row.names(frame)

stringsAsFactors_default_is_FALSE <-
isFALSE(as.logical(Sys.getenv("_R_OPTIONS_STRINGS_AS_FACTORS_")))

for(i in seq_len(d[2L])) {
xi <- frame[[i]]
## at present is.numeric suffices, but let's be cautious
Expand All @@ -37,7 +34,7 @@ data.matrix <- function(frame, rownames.force = NA)
frame[[i]] <- as.integer(xi)
next
}
if(stringsAsFactors_default_is_FALSE && is.character(xi)) {
if(is.character(xi)) {
frame[[i]] <- as.integer(factor(xi))
next
}
Expand Down
7 changes: 4 additions & 3 deletions src/library/base/man/data.matrix.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% File src/library/base/man/data.matrix.Rd
% Part of the R package, https://www.R-project.org
% Copyright 1995-2008 R Core Team
% Copyright 1995-2019 R Core Team
% Distributed under GPL 2 or later

\name{data.matrix}
Expand All @@ -17,15 +17,16 @@ data.matrix(frame, rownames.force = NA)
}
\arguments{
\item{frame}{a data frame whose components are logical vectors,
factors or numeric vectors.}
factors or numeric or character vectors.}
\item{rownames.force}{logical indicating if the resulting matrix
should have character (rather than \code{NULL})
\code{\link{rownames}}. The default, \code{NA}, uses \code{NULL}
rownames if the data frame has \sQuote{automatic} row.names or for a
zero-row data frame.}
}
\details{
Logical and factor columns are converted to integers. Any other
Logical and factor columns are converted to integers. Character
columns are first converted to factors and then to integers. Any other
column which is not numeric (according to \code{\link{is.numeric}}) is
converted by \code{\link{as.numeric}} or, for S4 objects,
\code{\link{as}(, "numeric")}. If all columns are integer (after
Expand Down

0 comments on commit d1d3863

Please sign in to comment.