Skip to content

Documentation for pivot_longer() - ptypes vs transform #1364

@rjake

Description

@rjake

I love the new(ish) pivot_*() functions. Although I use them a lot, I don't understand when to use ptypes and when to use transform. Could the documentation include examples for each? Data class mismatches are the most common error I face and I found suggestions to use each of these when looking on stackoverflow

library(tidyverse)

# no
mpg |> pivot_longer(cols = -year, values_ptypes = as.character())
mpg |> pivot_longer(cols = -year, values_ptypes = character())
mpg |> pivot_longer(cols = -year, values_ptypes = list(value = "character"))
# Error: Can't convert <double> to <character>.

# yes
mpg |> pivot_longer(cols = -year, values_transform = as.character)
# year  name          value   
# <int> <chr>         <chr>   
# 1999  manufacturer  audi    
# 1999  model         a4      
# 1999  displ         1.8     
# 1999  cyl           4   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions