Skip to content

Passing expressions as columns to pivot_longer fails with error.  #118

@barryrowlingson

Description

@barryrowlingson

Describe the bug

Running pivot_longer with column names specified in a vector with c("Y1","Y2") works fine, but if an expression returns that vector it fails.

To Reproduce

Sample data:

d = data.frame(X=1:5, Y1=runif(5), Y2=runif(5))

I want to pivot longer on all vars except the first one. The names are therefore:

names(d)[-1]
[1] "Y1" "Y2"
nd = names(d)[-1]

and this works

pivot_longer(d, nd)
   X name     value
1  1   Y1 0.9821147
2  1   Y2 0.8756433
3  2   Y1 0.9908365

but this doesn't:

> pivot_longer(d, names(d)[-1])
Error in (function (.data, ..., .group_pos = FALSE)  : 
  Locations Y1 and Y2 don't exist. There are only 3 columns.

I could simply do pivot_longer(d, -1) but there are maybe other contexts where an expression might get passed to cols, eg some function that returns the columns:

> getY = function(){c("Y1","Y2")}
> pivot_longer(d, getY())
Error in (function (.data, ..., .group_pos = FALSE)  : 
  Locations Y1 and Y2 don't exist. There are only 4 columns.

Expected behavior
The two pivot_longer calls should return the same data frame.

System Information:
Please detail the following information

  • R.version.string is [1] "R version 4.1.1 (2021-08-10)"
  • packageVersion("poorman") is 0.2.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions