Skip to content

Apply universal renaming after unnest() #514

@krlmlr

Description

@krlmlr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementrectangling 🗄️converting deeply nested lists into tidy data frames

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions