2017-04-20 6 views
5

tidyrパッケージのgather()関数に問題があります。一意の名前を持つときに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

第2引数と第3引数は、出力に作成されるキーと値の列の名前です。同じ名前の2つの列が奇数で、tidyrまたはdplyrという他の関数ではうまく機能しません。私は新しい列に他の名前を付けることをお勧めします。したがって、あなたが試すことができます:あなたのデータを見ているとき

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

私が参照してください。私はエラーがデータフレームのフィールド内で重複した値を選んだと思った。私には起こらなかった、私はfuncitonと名前を複製していた。ありがとう! – Prometheus

1

エラーメッセージは、新しい列market_shareを作成しようとしていますが、すでに存在していることを示しています。あなたが作成しようとしている列なので、periodを2番目の場所に置く必要があります。

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 
... 
関連する問題