mtcars = as.data.table(mtcars)
iris = as.data.table(iris)
# examples for nest
# nest by which columns?
mtcars %>% nest(cyl)
mtcars %>% nest("cyl")
mtcars %>% nest(cyl,vs)
mtcars %>% nest(vs:am)
mtcars %>% nest("cyl|vs")
mtcars %>% nest(c("cyl","vs"))
# nest two columns directly
iris %>% nest(mcols = list(petal="^Pe",sepal="^Se"))
# nest more flexibly
iris %>% nest(mcols = list(ndt1 = 1:3,
ndt2 = "Pe",
ndt3 = Sepal.Length:Sepal.Width))
# examples for unnest
# unnest which column?
mtcars %>% nest("cyl|vs") %>%
unnest(ndt)
mtcars %>% nest("cyl|vs") %>%
unnest("ndt")
df <- data.table(
a = list(c("a", "b"), "c"),
b = list(c(TRUE,TRUE),FALSE),
c = list(3,c(1,2)),
d = c(11, 22)
)
df
df %>% unnest(a)
df %>% unnest(2)
df %>% unnest("c")
df %>% unnest(cols = names(df)[3])
# You can unnest multiple columns simultaneously
df %>% unnest(1:3)
df %>% unnest(a,b,c)
df %>% unnest("a|b|c")
# examples for squeeze
# nest which columns?
iris %>% squeeze(1:2)
iris %>% squeeze("Se")
iris %>% squeeze(Sepal.Length:Petal.Width)
# examples for chop
df <- data.table(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1)
df %>% chop(y,z)
df %>% chop(y,z) %>% unchop(y,z)
Run the code above in your browser using DataLab