2017-04-20 26 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 
...