-
Notifications
You must be signed in to change notification settings - Fork 996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closes #2930 -- bugfix to as.matrix.data.table() #2938
Changes from all commits
0389de1
b1590ae
1323be0
ddaeb6a
0ab7e4f
8477788
ac52d9a
b9eab65
c0cca0d
11da144
5b4bca7
c5ae94b
f07b813
3d4681c
d9a4a54
de48d84
2810538
8348172
12cace8
895554e
a887594
cde74a2
6afb012
5be4459
3666b9c
9add536
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1887,14 +1887,19 @@ chmatch2 <- function(x, table, nomatch=NA_integer_) { | |
# x | ||
#} | ||
|
||
as.matrix.data.table <- function(x, rownames, ...) { | ||
as.matrix.data.table <- function(x, rownames, rownames.literal=FALSE, ...) { | ||
rn <- NULL | ||
rnc <- NULL | ||
if (!missing(rownames)) { # Convert rownames to a column index if possible | ||
if (length(rownames) == nrow(x)) { | ||
if (length(rownames) == nrow(x) && nrow(x) > 1) { | ||
# rownames argument is a vector of row names, no column in x to drop. | ||
rn <- rownames | ||
rnc <- NULL | ||
} else if (nrow(x) == 1 && isTRUE(rownames.literal)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we need |
||
# When x has a single row, the user may still want to supply vector of rownames, | ||
# but we need to distinguish from the case when the rownames is a column of x. | ||
rn <- rownames | ||
rnc <- NULL | ||
} else if (!is.null(rownames) && length(rownames) != 1L) { # vector(0) will throw an error, but NULL will pass through | ||
stop(sprintf("rownames must be a single column in x or a vector of row names of length nrow(x)=%d", nrow(x))) | ||
} else if (!(is.null(rownames) || is.logical(rownames) || is.character(rownames) || is.numeric(rownames))) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ Converts a \code{data.table} into a \code{matrix}, optionally using one | |
of the columns in the \code{data.table} as the \code{matrix} \code{rownames}. | ||
} | ||
\usage{ | ||
\method{as.matrix}{data.table}(x, rownames, ...)} | ||
\method{as.matrix}{data.table}(x, rownames, rownames.literal, ...)} | ||
|
||
\arguments{ | ||
\item{x}{a \code{data.table}} | ||
|
@@ -17,6 +17,11 @@ the \code{rownames} in the returned \code{matrix}. If \code{TRUE} the | |
single column, otherwise the first column in the \code{data.table} will | ||
be used. Alternative a vector of length \code{nrow(x)} to assign as the | ||
row names of the returned \code{matrix}.} | ||
\item{rownames.literal}{optional, when x is a \code{data.table} that has | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before word "optional" I would put "logical" to clearly state argument type in front, so
to
|
||
only a single row, set \code{rownames.literal} to \code{TRUE} if you want | ||
to use the vector supplied to the \code{rownames} argument as the row | ||
names of the returned \code{matrix} instead of looking for \code{rownames} | ||
as a column of \code{x}.} | ||
\item{\dots}{additional arguments to be passed to or from methods.} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In such unwanted edge case we don't have to be consistent to data.frame by default, it is important to give option for consistency to data.frame, which new arg already provides, but we could tune its default.
Maybe new arg could have smart default like
nrow(x)==1L
? probably more correct would benrow(x)==1L && !missing(rownames) && is.character(rownames)
but it is a little bit messy, maybenrow(x)==1L
is sufficient in this case.