问题
It's shameful, but I still can't wrap my mind fully around tidyr, specifically gather(). I feel like I'm missing something fundamental.
If I run this tiny snippet of code
library(tidyr)
x <- data.frame(var1=letters[1:3], var2=LETTERS[7:9], var3=21:23)
gather(x, foo, value)
I get
> x
var1 var2 var3
1 a G 21
2 b H 22
3 c I 23
> gather(x, foo, value)
variable value
1 var1 a
2 var1 b
3 var1 c
4 var2 G
5 var2 H
6 var2 I
7 var3 21
8 var3 22
9 var3 23
Where does foo get used? Is this completely unnecessary? Am I tripping up because I'm thinking reshape style where you define the ID variables and the rest get melted whereas I should be thinking differently where you define the variables to gather and the rest are considered ID?
回答1:
This is a bug that occurs when reshape and tidyr are both loaded. It has been fixed in tidyr 0.3.1.
来源:https://stackoverflow.com/questions/32598113/why-doesnt-gather-use-the-key-variable-name