Skip to content

Instantly share code, notes, and snippets.

@stufield
Last active January 31, 2019 06:20
Show Gist options
  • Save stufield/76a4114a0bf2f001e1e3c9f17f5ea6ef to your computer and use it in GitHub Desktop.
Save stufield/76a4114a0bf2f001e1e3c9f17f5ea6ef to your computer and use it in GitHub Desktop.
Possible nest() bug when acting on grouped_df object and passing nesting columns
reprex::reprex({
library(magrittr)
library(tibble)
suppressMessages(library(tidyr))
suppressMessages(library(dplyr))
# Option 1
# Proper nesting of cyl using group_by
df1 <- mtcars %>%
group_by(cyl) %>%
nest()
df1
names(df1$data[[1]])
# Option 2
# Proper nesting of cyl using nest alone
df2 <- mtcars %>%
as_tibble() %>%
nest(-cyl)
df2
names(df2$data[[1]])
# Option 3
# Both. Assume you used dplyr::group_by(), forgot to
# ungroup and then wanted to tidyr::nest() on `gear`
df3 <- mtcars %>%
as_tibble() %>%
group_by(cyl) %>%
nest(-gear)
df3
names(df3$data[[1]])
# In this case the group_by supercedes the nest argument, which is ok
# The problem is `gear` is dropped from the nested data frame
# yet gear is not part of the nesting
# Expected (?) Behavior
mtcars %>%
as_tibble() %>%
tidyr::nest(-cyl, -gear)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment