-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Reprex and proposal below.
library(tidyfast)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# |- data ----
dat <- structure(
list(
id = c("11", "22"),
phase = c("a", "b"),
values = list(
structure(
list(
a = 0.0584563566053344,
b = 192,
c = "50%",
d = 1,
e = 0,
f = 0,
g = 0
),
row.names = c(NA, -1L),
class = c("tbl_df",
"tbl", "data.frame")
),
structure(
list(
c = "50%",
d = 465L,
e = 0,
g = 290514.430137519,
b = 10961.9288476965,
a = 0.359973896295374,
h = 1.46588348984196,
f = 119.108387941727
),
row.names = c(NA,
-1L),
class = c("tbl_df", "tbl", "data.frame")
)
)
),
row.names = c(NA,
-2L),
class = c("tbl_df", "tbl", "data.frame")
)
# |- current ----
dat %>%
tidyfast::dt_unnest(values)
#> Error in rbindlist(eval(col)): Item 2 has 8 columns, inconsistent with item 1 which has 7 columns. To fill missing columns use fill=TRUE.
# |- proposed ----
dt_unnest.default_edit <- function(dt_, col, fill = FALSE, ...){
if (isFALSE(data.table::is.data.table(dt_)))
dt_ <- data.table::as.data.table(dt_)
col <- substitute(col)
keep <- substitute(alist(...))
print(keep)
names <- colnames(dt_)
others <- names[-match(paste(col), names)]
rows <- sapply(dt_[[paste(col)]], NROW)
if (length(keep) > 1)
others <- others[others %in% paste(keep)[-1]]
others_dt <- dt_[, ..others]
classes <- sapply(others_dt, typeof)
keep <- names(classes)[classes != "list"]
others_dt <- others_dt[, ..keep]
others_dt <- lapply(others_dt, rep, times = rows)
dt_[, list(data.table::as.data.table(others_dt),
data.table::rbindlist(eval(col),
fill = fill))]
}
dat %>%
dt_unnest.default_edit(values, fill = TRUE)
#> alist()
#> id phase a b c d e f g h
#> 1: 11 a 0.05845636 192.00 50% 1 0 0.0000 0.0 NA
#> 2: 22 b 0.35997390 10961.93 50% 465 0 119.1084 290514.4 1.465883Created on 2020-04-05 by the reprex package (v0.3.0)
TysonStanley
Metadata
Metadata
Assignees
Labels
No labels