Skip to content

automatically name unnamed pluckers in hoist #837

Closed

Description

@mgirlich

Often one wants to name the columns produced by hoist() the same as the element in the list. So to make hoist() less verbose and more comfortable for interactive use I propose automatically naming the column as follows for unnamed arguments:

  • if the last part of the pluck specification is a character use that
  • if the last part of the pluck specification is a number paste the number to the last character part of the spec, e.g. list("film", 1) would produce the column name film_1.

Activity

  1. changed the title [-]automatically name any unnamed pluckers[/-] [+]automatically name unnamed pluckers in hoist[/+] on Dec 13, 2019
  2. hadley commented on Apr 1, 2020

    @hadley
    Member

    This is an interesting idea. What would you do if the pluck spec is a number? Or the number comes first?

  3. added
    featurea feature request or enhancement
    rectangling 🗄️converting deeply nested lists into tidy data frames
    on Apr 1, 2020
  4. mgirlich commented on Apr 2, 2020

    @mgirlich
    ContributorAuthor

    maybe there should be a short and a long name option, e.g. something like

    pluck spec short name long name
    list("people", "name") name people_name
    list("people", "name", 1) name_1 people_name_1
    list(1) ..1 ..1
    list(1, 2) ..1..2 ..1..2
    list(1, "people", "name") ..1_name ..1_people_name

    as the numbers in a spec will probably be small one might also use words for the, i.e. list(1, "name") gives first_name

  5. hadley commented on Apr 2, 2020

    @hadley
    Member

    That's starting to feel a bit too magical too me — I think there'd have to be one principle applied consistently.

  6. mgirlich commented on Apr 2, 2020

    @mgirlich
    ContributorAuthor

    Looking at it again I agree that it looks quite magical.

    One easy principle would be to simply use paste(pluck_spec, collapse = "_") for the long name version. Unfortunately, this generates non-syntactic names for leading numbers. But this could be resolved by adding a names_repair argument, so the user can choose how to handle this.

    For short names one would use basically the same strategy but keep all trailing numbers, i.e.
    list("people", "name", 1, 2) would give name_1_2.

    Do you like that better?

  7. jennybc commented on Apr 3, 2020

    @jennybc
    Member

    The case that seems like a no-brainer and a great place to start is when you pluck a string. That covers so much of the pain IMO. This is the case where I most feel like hoist() should be able to figure this out.

    That being said, I feel like we discussed this once in slack @hadley, and you objected? There is some potential for muddying the waters re: our quoting conventions.

    library(tidyverse)
    #> Warning: package 'tibble' was built under R version 3.6.2
    
    df <- tibble(
      character = c("Toothless", "Dory"),
      metadata = list(
        list(
          species = "dragon",
          color = "black",
          films = c(
            "How to Train Your Dragon",
            "How to Train Your Dragon 2",
            "How to Train Your Dragon: The Hidden World"
          )
        ),
        list(
          species = "blue tang",
          color = "blue",
          films = c("Finding Nemo", "Finding Dory")
        )
      )
    )
    
    df %>% 
      hoist(metadata, species = "species", color = "color")
    #> # A tibble: 2 x 4
    #>   character species   color metadata        
    #>   <chr>     <chr>     <chr> <list>          
    #> 1 Toothless dragon    black <named list [1]>
    #> 2 Dory      blue tang blue  <named list [1]>

    Created on 2020-04-03 by the reprex package (v0.3.0.9001)

  8. added this to the v1.1.0 milestone on Apr 21, 2020
  9. hadley commented on Apr 21, 2020

    @hadley
    Member

    Ok, let's start with just naming single string pluckers.

  10. added a commit that references this issue on Sep 19, 2025
    4ebdd1f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions