简体   繁体   English

当我有唯一的名字时,tidyr :: gather()会出错

[英]error with tidyr::gather() when I have unique names

I have an issue with the gather() function from the tidyr package. 我有来自tidyr包的gather()函数的问题。

sample
# A tibble: 5 × 6
  market_share      Y2012      Y2013      Y2014      Y2015      Y2016
         <chr>      <dbl>      <dbl>      <dbl>      <dbl>      <dbl>
1          KAB 0.23469425 0.23513725 0.23187590 0.22940831 0.22662625
2          BGD 0.21353096 0.21352769 0.20910574 0.20035900 0.19374223
3          NN 0.16891699 0.16204919 0.16272993 0.16388675 0.16154017
4         OG 0.07648682 0.07597078 0.07945966 0.07780233 0.08069057
5         Ha 0.05092648 0.05480555 0.06434457 0.07127716 0.08054208

If I try: 如果我尝试:

sample2 <- gather(sample, market_share, period, Y2012:Y2016)
Error: Each variable must have a unique name.
Problem variables: 'market_share'

However, each variable appears to have a unique name. 但是,每个变量似乎都有一个唯一的名称。

Ha  KAB  BGD  NN OG 
   1    1    1    1    1 

It appears to be a common issue people have with gather, but I don't get it. 这似乎是人们聚集的常见问题,但我不明白。

The second and third argument is the names of key and value column to be created in output. 第二个和第三个参数是要在输出中创建的键和值列的名称。 Having two columns with the same name is odd and doesn't work well with other functions of tidyr or dplyr . 具有相同名称的两列是奇数,并且与tidyrdplyr其他函数不dplyr I suggest giving other names for new columns. 我建议为新列提供其他名称。 Therefore, you can try: 因此,您可以尝试:

sample2 <- gather(sample, period, value, Y2012:Y2016)

The error message tells you that you are trying to create a new column market_share , but it already exists. 该错误消息告诉您正在尝试创建新列market_share ,但它已存在。 You need to put period in the second spot because that's the column you are trying to create. 您需要将period点放在第二个位置,因为这是您要创建的列。

df1<-read.table(text="market_share      Y2012      Y2013      Y2014      Y2015      Y2016
KAB 0.23469425 0.23513725 0.23187590 0.22940831 0.22662625
BGD 0.21353096 0.21352769 0.20910574 0.20035900 0.19374223
NN 0.16891699 0.16204919 0.16272993 0.16388675 0.16154017
OG 0.07648682 0.07597078 0.07945966 0.07780233 0.08069057
Ha 0.05092648 0.05480555 0.06434457 0.07127716 0.08054208",header=TRUE, stringsAsFactors=FALSE)

library(tidyr)    
gather(df1, period,market_share)

   market_share period market_share
1           KAB  Y2012   0.23469425
2           BGD  Y2012   0.21353096
3            NN  Y2012   0.16891699
4            OG  Y2012   0.07648682
5            Ha  Y2012   0.05092648
6           KAB  Y2013   0.23513725
7           BGD  Y2013   0.21352769
8            NN  Y2013   0.16204919
9            OG  Y2013   0.07597078
10           Ha  Y2013   0.05480555

When looking at your data, it seems that your data is a tibble object (see tibble::tibble). 查看数据时,您的数据似乎是一个tibble对象(请参阅tibble :: tibble)。 But gather requires a data.frame. 但是gather需要data.frame。 Try to change your object to a data.frame: 尝试将您的对象更改为data.frame:

sample2 <- gather(data.frame(sample),market_share, period, Y2012:Y2016)

This should fix your problem. 这应该可以解决您的问题。

Example: 例:

library(tibble)
sample <- read.table(text="market_share Y2012 Y2013 Y2014 Y2015 Y2016
KAB 0.23469425 0.23513725 0.23187590 0.22940831 0.22662625
BGD 0.21353096 0.21352769 0.20910574 0.20035900 0.19374223
NN 0.16891699 0.16204919 0.16272993 0.16388675 0.16154017
OG 0.07648682 0.07597078 0.07945966 0.07780233 0.08069057
Ha 0.05092648 0.05480555 0.06434457 0.07127716 0.08054208",
header=TRUE, stringsAsFactors=FALSE)  

sample <- as_tibble(sample)  
sample

# A tibble: 5 x 6
  market_share      Y2012      Y2013      Y2014      Y2015      Y2016
         <chr>      <dbl>      <dbl>      <dbl>      <dbl>      <dbl>
1          KAB 0.23469425 0.23513725 0.23187590 0.22940831 0.22662625
2          BGD 0.21353096 0.21352769 0.20910574 0.20035900 0.19374223
3           NN 0.16891699 0.16204919 0.16272993 0.16388675 0.16154017
4           OG 0.07648682 0.07597078 0.07945966 0.07780233 0.08069057
5           Ha 0.05092648 0.05480555 0.06434457 0.07127716 0.08054208

sample2 <- gather(sample, period, result, Y2012:Y2016) # Does not work
Error: Column 'market_share' must have a unique name

This doesn't work but if you change it into a data.frame , it works: 这不起作用,但如果您将其更改为data.frame ,它可以工作:

sample2 <- sample2 <- gather(data.frame(sample), period, result, Y2012:Y2016) # works perfect`
sample2
       market_share period     result
1           KAB  Y2012 0.23469425
2           BGD  Y2012 0.21353096
3            NN  Y2012 0.16891699
4            OG  Y2012 0.07648682
5            Ha  Y2012 0.05092648
6           KAB  Y2013 0.23513725
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM