Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gather_ does not work. Shouldn't quoting and ~ing have the same effect in standard evaluation mode?

Tags:

r

dplyr

tidyr

I have issues getting tidyr's gather to work in it's standard evaluation version gather_ :

require(tidyr)
require(dplyr)
require(lazyeval)

df = data.frame(varName=c(1,2))

gather works:

df %>% gather(variable,value,varName)

but I'd like to be able to take the name varName from a variable in standard evaluation mode, and can't seem to get it right:

name='varName'
df %>% gather_("variable","value",interp(~v,v=name))
Error in match(x, y, 0L) : 'match' requires vector arguments

I'm also confused by the following.

This works as expected:

df %>% gather_("variable","value","varName")

The next line should be equivalent to last line (from my understanding of http://cran.r-project.org/web/packages/dplyr/vignettes/nse.html ), but doesn't work:

df %>% gather_(~variable,~value,~varName)
Error in match(x, y, 0L) : 'match' requires vector arguments
like image 900
hansonhill Avatar asked Apr 09 '15 11:04

hansonhill


1 Answers

Looking at the source of tidyr:::gather_.data.frame, you can see that it is just a wrapper for reshape2::melt. As such, it only works for character or numeric arguments. Acutally the following (which I would consider a bug) works:

df %>% gather_("variable", "value", 1)

As far as I can tell the nse vignette only refers to dplyr and not to tidyr.

like image 52
shadow Avatar answered Nov 04 '22 21:11

shadow