Stop gather function from dropping factor labels

心已入冬 提交于 2019-12-02 05:47:16

问题


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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!