无法在 R 中收集 tibble
Can't gather tibble in R
您是否看到使用 tidyr::gather
函数收集 tibble
class 的最新问题?在 data.frame
上使用简单的 select
或 rename
函数后,现在 dplyr
returns tibble
.
除了将 tibble
class 更改为 data.frame
之外,还有什么方法可以解决这个问题吗?
下面是简单的例子
> 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
问题不一定出在 tibble
上。它正在做不允许重复的列名的安全(并且可以说是非常严格的)事情。
这意味着您的 gather
调用应避免重名。这可以通过自己为 key
和 value
列提供名称来实现,例如:
gather(iris, key, value, -Species)
您是否看到使用 tidyr::gather
函数收集 tibble
class 的最新问题?在 data.frame
上使用简单的 select
或 rename
函数后,现在 dplyr
returns tibble
.
除了将 tibble
class 更改为 data.frame
之外,还有什么方法可以解决这个问题吗?
下面是简单的例子
> 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
问题不一定出在 tibble
上。它正在做不允许重复的列名的安全(并且可以说是非常严格的)事情。
这意味着您的 gather
调用应避免重名。这可以通过自己为 key
和 value
列提供名称来实现,例如:
gather(iris, key, value, -Species)