Commit 83dbd3c1 authored by Quentin Read's avatar Quentin Read
Browse files

updates to nesting fns

parent afa5762c
#' Nest a data.table by group
#'
#' This function was written by TS Barrett in a document available at https://osf.io/f6pxw/download.
#' This function was originally written by TS Barrett in a document available at https://osf.io/f6pxw/download.
#' QDR edited so that it can also accept a character vector (good for programmatic usage of the function)
#'
#' @import data.table
#' @export
group_nest_dt <-function(dt, ..., .key = "data") {
stopifnot(is.data.table(dt))
by <-substitute(list(...))
if (missing(group_vars)) {
by <-substitute(list(...))
dt <- dt[, list(list(.SD)), by = eval(by)]
} else {
dt <- dt[, list(list(.SD)), by = c(group_vars)]
}
dt <- dt[, list(list(.SD)), by = eval(by)]
setnames(dt, old = "V1", new = .key)
dt
}
......@@ -25,5 +31,5 @@ unnest_dt <-function(dt, col, id) {
by <-substitute(id)
col <-substitute(unlist(col, recursive = FALSE))
dt[, eval(col), by = eval(by)]
dt[, eval(col), by = by]
}
......@@ -7,5 +7,6 @@
group_nest_dt(dt, ..., .key = "data")
}
\description{
This function was written by TS Barrett in a document available at https://osf.io/f6pxw/download.
This function was originally written by TS Barrett in a document available at https://osf.io/f6pxw/download.
QDR edited so that it can also accept a character vector (good for programmatic usage of the function)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment