I'm trying to use the gather function in tidyr - but it is stripping out the labels from factored data. My data looks something like this:
> require(tidyr)
> messy = data.frame(x=rep(seq(0,2),2),y=runif(6),z=runif(6),source=c('good','bad'))
> messy
x y z source
1 0 0.37627685 0.9108316 good
2 1 0.77593147 0.9944256 bad
3 2 0.01105364 0.1183923 good
4 0 0.37755463 0.6761343 bad
5 1 0.86333114 0.7312482 good
6 2 0.69085345 0.8288506 bad
>tidy = gather(messy,coordinate,value,y:z)
>tidy
x source coordinate value
1 0 2 y 0.37627685
2 1 1 y 0.77593147
3 2 2 y 0.01105364
4 0 1 y 0.37755463
5 1 2 y 0.86333114
6 2 1 y 0.69085345
7 0 2 z 0.91083162
8 1 1 z 0.99442560
9 2 2 z 0.11839230
10 0 1 z 0.67613427
11 1 2 z 0.73124818
12 2 1 z 0.82885055
Tidy has gathered the y
and z
variables as advertised, but the source
variable is now an integer.
What am I doing wrong?
This is a bug reported here: https://github.com/hadley/tidyr/issues/104
It seems like it was fixed in tidyr 0.3.1
来源:https://stackoverflow.com/questions/32496306/stop-gather-function-from-dropping-factor-labels