Issues with pivot_longer

Hi everyone!
I would really appreciate if anyone can guide me towards the right direction.
I am trying to run this code on Rstudio

gap_long <- gap_wide %>%
  pivot_longer(
    cols = c(starts_with('pop'), starts_with('lifeExp'), starts_with('gdpPercap')),
    names_to = "obstype_year", values_to = "obs_values"
  )
str(gap_long)

But it gives me this error:

Error in pivot_longer(., cols = c(starts_with("pop"), starts_with("lifeExp"), :
could not find function "pivot_longer"

I've tried installing tidyr but still no hope.

Hi, @ccelispuga. Are you sure you have loaded the tidyr package using the library(tidyr) function call or perhaps through library(tidyverse)?

Hiii, thank you in advance for responding so quickly!

I made the wise decision to take a coding class for the winter session & freaking out right now because it doesn't want too work.

Let me try again & hopefully it works!!

I did, I used "tidyr" & ran the code--still giving me an error

library(tidyr)
Warning message:
package ‘tidyr’ was built under R version 3.4.4

gap_long <- gap_wide %>%

  • pivot_longer(
  • cols = c(starts_with('pop'), starts_with('lifeExp'), starts_with('gdpPercap')),
    
  • names_to = "obstype_year", values_to = "obs_values"
    
  • )
    Error in pivot_longer(., cols = c(starts_with("pop"), starts_with("lifeExp"), :
    could not find function "pivot_longer"

str(gap_long)
Error in str(gap_long) : object 'gap_long' not found

Try running the command

sessionInfo()

in the console. In the section other attached packages, what does it say about tidyr? Mine shows

tidyr_1.0.0

which means I have version 1.0.0 of tidyr.

other attached packages:
[1] tidyr_0.8.3

pivoy_longer() is a new function, you have to update tidyr in order to get it.

install.packages("tidyr")

Also, as a side note, it seems like you are using a quite old R version, it would make things easier for you if you update it as well, current is version is 3.6.2.

I have version
R version 3.4.2 (2017-09-28)"

I typed the common above: install.packages ('tidyr") and this is what I received

I just downloaded the software yesterday

It is asking you to update rlang package since it is a package dependency for tidyr, try restarting your R session and updating rlang first and then tidyr.

install.packages("rlang")

And as I said before, package installation would be much easier for you if you update R to the latest version, because you would have access to binary versions to avoid compiling.

You can download the latest version (3.6.2) for macOS (and the recommended complementary software) here
https://cran.r-project.org/bin/macosx/

I tried running install.packages ("

rlang")

I am using Rstudio, I think the newer version (which i did download) isn't for R studio

Sorry I don't understand what you mean, it doesn't make much sense, RStudio is an IDE for the R programming language, but they are two independent pieces of software, each one can have independent versions.
The error message above is telling you that you need to update R (not RStudio) in order to install the latest rlang version, and you need that to update tidyr

Ohhh, I see (sorry coding was not one of my strong suits as a undergrad)
I updated the version, through the link you attached. I will re-start the R session hopefully that will work!

I think it WORKED!!!!
25

I have been trying to figure this out since 12 pm, THANK YOU!!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.