Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply universal renaming after unnest() #514

Closed
krlmlr opened this issue Oct 25, 2018 · 2 comments

Comments

@krlmlr
Copy link
Member

commented Oct 25, 2018

Otherwise, duplicate column names may lead to hard-to-detect failures.

In the example below, an early mutate() that adds a column that exists in the sub-tibble breaks code that worked previously in a way that may go unnoticed. With universal names, the failure is detected immediately.

library(tidyverse)
data <- tibble(x = 1, b = list(tibble(a = 2:3)))
data
#> # A tibble: 1 x 2
#>       x b               
#>   <dbl> <list>          
#> 1     1 <tibble [2 × 1]>

old_data <-
  data %>%
  unnest()

old_data
#> # A tibble: 2 x 2
#>       x     a
#>   <dbl> <int>
#> 1     1     2
#> 2     1     3
old_data %>% filter(a == 2)
#> # A tibble: 1 x 2
#>       x     a
#>   <dbl> <int>
#> 1     1     2

new_data <-
  data %>%
  mutate(a = 1) %>% # This breaks existing code!
  unnest()

new_data
#> # A tibble: 2 x 3
#>       x     a    a1
#>   <dbl> <dbl> <int>
#> 1     1     1     2
#> 2     1     1     3
new_data %>% filter(a == 2)
#> # A tibble: 0 x 3
#> # ... with 3 variables: x <dbl>, a <dbl>, a1 <int>

# Suggested fix: Safe failure

new_data <-
  new_data %>%
  rename(a..2 = a, a..3 = a1) %>%
  filter(a == 2)
#> Error: Evaluation error: object 'a' not found.

Created on 2018-10-25 by the reprex package (v0.2.1.9000)

Tested with c318c31.

@krlmlr

This comment has been minimized.

Copy link
Member Author

commented Oct 25, 2018

On the other hand, the .sep argument already provides a somewhat safe solution for programming:

library(tidyverse)
data <- tibble(x = 1, b = list(tibble(a = 2:3)))

data %>%
  mutate(a = 1) %>%
  unnest(.sep = ".")
#> # A tibble: 2 x 3
#>       x     a   b.a
#>   <dbl> <dbl> <int>
#> 1     1     1     2
#> 2     1     1     3

Created on 2018-10-25 by the reprex package (v0.2.1.9000)

@hadley

This comment has been minimized.

Copy link
Member

commented Apr 23, 2019

Currently implemented in unnest2(), which I'm going to re-unify with unnest() shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.