0

I was trying to rearrange a dataframe using dataframe %>% pivot_longer( ) but would keep getting errors however I tried to specify the cols argument.

For instance, trying cols = (!colname) I'd get: Error: no applicable method for 'pivot_longer' applied to an object of class "logical" In addition: ‘!’ not meaningful for factors

Using starts_with("string") I'd get: Error: starts_with() must be used within a selecting function.

(Even though the example given on the help page for usage of 'starts_with()' gives "The cols argument of tidyr::pivot_longer()" as an example of where it should be used!)

And trying cols = c(2:18) gave me: no applicable method for 'pivot_longer' applied to an object of class "c('integer', 'numeric')

However, putting the name of the dataframe as the first argument within the brackets seems to have fixed the problem. I'm totally puzzled. I already tried library(tidyr) to check that no functions were being masked. Any idea what I could be doing wrong?

Ellen
  • 33
  • 5
  • 1
    The `pivot_longer()` function does work in a pipeline. My guess is that it's something about the way you're calling the columns. If you could post a reproducible example, we could likely help you out. – DaveArmstrong Feb 04 '21 at 17:17
  • Thanks, will do. I'm now getting another problem with another dplyr function not seeming to recognise dplyr syntax- exactly the same as the one asked here: https://stackoverflow.com/questions/64012121/summarise-error-n-must-only-be-used-inside-dplyr-verbs?noredirect=1&lq=1 – Ellen Feb 04 '21 at 17:19
  • `(.packages())` will show you what libraries are loaded, your linked message had `plyr` and `dplyr` loaded at the same time. Can you post a reproducible example, maybe the example from the help page, or using the iris dataset. – M.Viking Feb 04 '21 at 17:30
  • Seems to have been fixed by reopening RStudio and reloading packages! Thank you :) – Ellen Feb 04 '21 at 19:18

0 Answers0