2017-04-20 47 views
5

我有一个问题,从tidyr包收集()函数。错误与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 

如果我尝试:

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

然而,每个变量似乎有一个唯一的名称。

Ha KAB BGD NN OG 
    1 1 1 1 1 

这似乎是人们聚集的一个共同的问题,但我不明白。

回答

5

第二个和第三个参数是要在输出中创建的键和值列的名称。具有两个同名的列是奇数,并且与tidyrdplyr的其他函数不兼容。我建议给新的列的其他名称。因此,你可以尝试:

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

我明白了。我认为这个错误在数据框的字段中单独出现了重复的值。没有发生在我身上我正在重复与功能的名称。谢谢! – Prometheus

1

错误消息告诉你,你要创建一个新列market_share,但它已经存在。您需要将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 
0

当你的数据看,似乎你的数据是tibble对象(见tibble :: tibble)。但是gather需要一个data.frame。 尝试你的对象更改为data.frame:

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

这应该可以解决您的问题。

例子:

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 

这并不工作,但如果你把它变成一个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 
...