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

Rectangling tools for tree like data frames #1384

Closed
mgirlich opened this issue Aug 25, 2022 · 4 comments
Closed

Rectangling tools for tree like data frames #1384

mgirlich opened this issue Aug 25, 2022 · 4 comments

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Aug 25, 2022

There are some nice rectangling tools in tidyr but I regularly miss a tool to rectangle/flatten tree like (or recursive) data frames. For example I want to get from

df <- tibble::tibble(
  id = 1:2,
  x = c("a", "b"),
  children = list(
    tibble::tibble(
      id = 3L,
      x = "c",
      children = list(
        tibble::tibble(
          id = 5L,
          x = "e",
          children = list(NULL)
        )
      )
    ),
    tibble::tibble(
      id = 4,
      x = "d",
      children = list(NULL)
    )
  )
)

df
#> # A tibble: 2 × 3
#>      id x     children        
#>   <int> <chr> <list>          
#> 1     1 a     <tibble [1 × 3]>
#> 2     2 b     <tibble [1 × 3]>
df$children
#> [[1]]
#> # A tibble: 1 × 3
#>      id x     children        
#>   <int> <chr> <list>          
#> 1     3 c     <tibble [1 × 3]>
#> 
#> [[2]]
#> # A tibble: 1 × 3
#>      id x     children
#>   <dbl> <chr> <list>  
#> 1     4 d     <NULL>
df$children[[1]]$children
#> [[1]]
#> # A tibble: 1 × 3
#>      id x     children
#>   <int> <chr> <list>  
#> 1     5 e     <NULL>

Created on 2022-08-25 with reprex v2.0.2

to the following

tibble::tibble(
  id = c(1L, 2L, 3L, 5L, 4L),
  x = c("a", "b", "c", "e", "d"),
  level = c(1L, 1L, 2L, 2L, 3L),
  parent = c(NA, NA, 1L, 2L, 3L)
)
#> # A tibble: 5 × 4
#>      id x      level parent
#>   <int> <chr>  <int>  <int>
#> 1     1 a          1     NA
#> 2     2 b          1     NA
#> 3     3 c          2      1
#> 4     5 e          2      2
#> 5     4 d          3      3

Created on 2022-08-25 with reprex v2.0.2

Of course this can be done with the existing tools of tidyr but it is a bit tedious. It would be great to add unnest_tree() and nest_tree() to handle this kind of data.

This was referenced Aug 25, 2022
@hadley
Copy link
Member

hadley commented Oct 19, 2022

Where do you encounter this sort of data? I've never seen it before, which makes me suspect this code is better off in a separate package.

@mgirlich
Copy link
Contributor Author

Examples would be a directory structure or a category tree that you get from an API. You parse the JSON and you have this tree structure.
It is more specialised than the recently added purrr::modify_tree() (tidyverse/purrr#720) but to me this feels like a nice addition.

@hadley
Copy link
Member

hadley commented Oct 20, 2022

In general, we're cautious about adding tools for workflows that we don't experience, so given that I have no intuition for this type of data, I'd prefer to keep it out of tidyr for now.

@hadley
Copy link
Member

hadley commented Dec 21, 2022

Closing this for the above reasons; I think it's an interesting idea, but doesn't feel right for tidyr yet.

@hadley hadley closed this as completed Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants