-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows creating data.table rowwisely (#4291)
* add a new argument .rowwise to data.table() which allows creating a data.table object rowwisely * reimplment rowwiseDT (uses `name=` synax) * export rowwiseDT() * update the docs using rowwiseDT * update NEWs * update and improve the tests * avoid using the base function name as the variable name * use ncols to avoid name collision to base::ncol * doc rowwiseDT() in a seperated Rd file * sprintf -> gettextf * remove the key argument * tweak the doc * tweak the error message * re-site NEWS * modernize: stopf() * grammar, style * trailing ws * Give a proper shout-out to tibble::tribble() * correct message text in test * move to new R/ file * slightly simpler, remove lambda * don't repeat calculation of nrows (also more readable) * also list() up 0-length --------- Co-authored-by: Michael Chirico <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
- Loading branch information
1 parent
97980d9
commit b566822
Showing
6 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
rowwiseDT = function(...) { | ||
x = substitute(list(...))[-1L] | ||
if (is.null(nms <- names(x))) | ||
stopf("Must provide at least one column (use `name=`). See ?rowwiseDT for details") | ||
header_pos = which(nzchar(nms)) | ||
if (any(nzchar(x[header_pos]))) | ||
stopf("Named arguments must be empty") | ||
if (!identical(header_pos, seq_along(header_pos))) | ||
stopf("Header must be the first N arguments") | ||
header = nms[header_pos] | ||
ncols = length(header) | ||
body = lapply(x[-header_pos], eval, envir = parent.frame()) | ||
nrows = length(body) %/% ncols | ||
if (length(body) != nrows * ncols) | ||
stopf("There are %d columns but the number of cells is %d, which is not an integer multiple of the columns", ncols, length(body)) | ||
# make all the non-scalar elements to a list | ||
needs_list = lengths(body) != 1L | ||
body[needs_list] = lapply(body[needs_list], list) | ||
body = split(body, rep(seq_len(nrows), each = ncols)) | ||
ans = rbindlist(body) | ||
setnames(ans, header) | ||
ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
\name{rowwiseDT} | ||
\alias{rowwiseDT} | ||
\title{ Create a data.table row-wise } | ||
\description{ | ||
\code{rowwiseDT} creates a \code{data.table} object by specifying a row-by-row layout. This is convenient and highly readable for small tables. | ||
} | ||
\usage{ | ||
rowwiseDT(...) | ||
} | ||
\arguments{ | ||
\item{...}{ Arguments that define the structure of a \code{data.table}. The column names come from named arguments (like \code{col=}), which must precede the data. See Examples. } | ||
} | ||
\value{ | ||
A \code{data.table}. The default is for each column to return as a vector. However, if any entry has a length that is not one (e.g., \code{list(1, 2)}), the whole column will be converted to a list column. | ||
} | ||
\seealso{ | ||
\code{\link{data.table}} | ||
} | ||
\examples{ | ||
rowwiseDT( | ||
A=,B=, C=, | ||
1, "a",2:3, | ||
2, "b",list(5) | ||
) | ||
} |