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
2b79efa7
Commit
2b79efa7
authored
Feb 12, 2021
by
Quentin Read
Browse files
ts barrett's functions to nest and unnest data.tables
parent
62a90776
Changes
1
Hide whitespace changes
Inline
Side-by-side
group_nest_dt.R
0 → 100644
View file @
2b79efa7
# Define function to nest DT by group, and unnest.
# these functions were written by TS Barrett in a document available at https://osf.io/f6pxw/download
group_nest_dt
<-
function
(
dt
,
...
,
.key
=
"data"
){
stopifnot
(
is.data.table
(
dt
))
by
<-
substitute
(
list
(
...
))
dt
<-
dt
[,
list
(
list
(
.SD
)),
by
=
eval
(
by
)]
setnames
(
dt
,
old
=
"V1"
,
new
=
.key
)
dt
}
unnest_dt
<-
function
(
dt
,
col
,
id
){
stopifnot
(
is.data.table
(
dt
))
by
<-
substitute
(
id
)
col
<-
substitute
(
unlist
(
col
,
recursive
=
FALSE
))
dt
[,
eval
(
col
),
by
=
eval
(
by
)]
}
\ No newline at end of file
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