Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ci-misc
R-utility-belt
Commits
83dbd3c1
Commit
83dbd3c1
authored
Feb 16, 2021
by
Quentin Read
Browse files
updates to nesting fns
parent
afa5762c
Changes
2
Hide whitespace changes
Inline
Side-by-side
R/group_nest_dt.R
View file @
83dbd3c1
#' Nest a data.table by group
#' 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
#' @import data.table
#' @export
#' @export
group_nest_dt
<-
function
(
dt
,
...
,
.key
=
"data"
)
{
group_nest_dt
<-
function
(
dt
,
...
,
.key
=
"data"
)
{
stopifnot
(
is.data.table
(
dt
))
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
)
setnames
(
dt
,
old
=
"V1"
,
new
=
.key
)
dt
dt
}
}
...
@@ -25,5 +31,5 @@ unnest_dt <-function(dt, col, id) {
...
@@ -25,5 +31,5 @@ unnest_dt <-function(dt, col, id) {
by
<-
substitute
(
id
)
by
<-
substitute
(
id
)
col
<-
substitute
(
unlist
(
col
,
recursive
=
FALSE
))
col
<-
substitute
(
unlist
(
col
,
recursive
=
FALSE
))
dt
[,
eval
(
col
),
by
=
eval
(
by
)
]
dt
[,
eval
(
col
),
by
=
by
]
}
}
man/group_nest_dt.Rd
View file @
83dbd3c1
...
@@ -7,5 +7,6 @@
...
@@ -7,5 +7,6 @@
group_nest_dt(dt, ..., .key = "data")
group_nest_dt(dt, ..., .key = "data")
}
}
\description{
\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)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment