dcl-docs / book

Book: Template for a bookdown book
https://dcl-book.stanford.edu
Other
1 stars 1 forks source link

pivot_longer names_ptypes not working as described #1

Closed ajacoby closed 2 years ago

ajacoby commented 3 years ago

https://dcl-wrangle.stanford.edu/pivot_advanced.html has the example:

example_eagle_nests %>% 
  pivot_longer(
    cols = c(`2007`, `2009`), 
    names_to = "year", 
    names_ptypes = list(year = integer()),
    values_to = "num_nests"
  )

However, when I run this example I get the error: Error: Can't convert <character> to <integer>.

Searching on this error leads to the page https://www.reddit.com/r/Rlanguage/comments/gu4ygj/tidyverse_nams_ptypes_argument_in_the_pivot/ which suggests the solution of using the names_transform argument instead. This worked for me, replacing names_ptypes = list(year = integer()) with names_transform = list(year = as.integer).

I'm new to R and wondering if it has to do with the version of R that I'm using or what. I looked in the Welcome and Basics and didn't see a specification of which version of R (or tidyr) the text applies to. Perhaps the library has been updated since the text was written? I believe I'm using version 1.1.2 of tidyr and R 4.0.3.

Thanks for sharing your excellent materials freely online!

behrman commented 3 years ago

Yes, you are right. An update to tidyr broke the old code, and the new code should be:

example_eagle_nests %>% 
  pivot_longer(
    cols = c(`2007`, `2009`), 
    names_to = "year", 
    names_transform = list(year = as.integer),
    values_to = "num_nests"
  )

We will be updating all of our books in upcoming weeks to the most recent versions of the Tidyverse packages, and we will include these changes.

Thanks for pointing this out!

behrman commented 2 years ago

Fixed. Thanks!