Marcin Kosiński Marcin Kosiński - 3 months ago 11
R Question

Can't gather tibble in R

Have you seen the newest problem with gathering

tibble
class with the use of
tidyr::gather
funciton? Now
dplyr
returns
tibble
after you use simple
select
or
rename
function on a
data.frame
.

Is there any way to workaround this instead of changing
tibble
class to
data.frame
?

Simple example is below

> library(tibble)
> library(tidyr)
>
> head(gather(iris, Species))
Species Species value
1 setosa Sepal.Length 5.1
2 setosa Sepal.Length 4.9
3 setosa Sepal.Length 4.7
4 setosa Sepal.Length 4.6
5 setosa Sepal.Length 5.0
6 setosa Sepal.Length 5.4
>
> head(gather(as_tibble(iris), Species))
Error: Each variable must have a unique name.
Problem variables: 'Species'
>
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

locale:
[1] LC_CTYPE=pl_PL.UTF-8 LC_NUMERIC=C LC_TIME=pl_PL.UTF-8
[4] LC_COLLATE=pl_PL.UTF-8 LC_MONETARY=pl_PL.UTF-8 LC_MESSAGES=pl_PL.UTF-8
[7] LC_PAPER=pl_PL.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=pl_PL.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] tidyr_0.6.0 tibble_1.2

loaded via a namespace (and not attached):
[1] magrittr_1.5 assertthat_0.1 tools_3.3.1 Rcpp_0.12.6

Answer

The issue is not necessarily with tibble. It is doing the safe (and arguably very restrictive) thing of not allowing duplicated column names.

This means your gather calls should avoid duplicated names. This can be achieved by supplying names for the key and value columns yourself, e.g.:

gather(iris, key, value, -Species)
Comments