2016-07-19 6 views
2

私は以下のようなデータを持っていますので、長い形式に変換したいと思います。R:データをワイドからロングに変換する - 複数の条件 - エラーが発生する

id count a1  b1 c1 a2  b2 c2 a3 b3 c3 age 
1 1  apple 2 3 orange 3 2 beer 2 1 50 
1 2  orange 3 2 apple 2 2 beer 2 1 50 
2 1  pear 3 2 apple 2 2 orange 2 2 45 

[A1、B1、C1]、[A2、B2、C2]、[A3、B3、C3]割り当てられたIDの人が直面している3つの属性のセットがあり、この人は、複数の直面するかもしれませんi番目の選択状況を示すカウントを持つ選択状況。私は、次のような他の変数を維持しながら、長い形式に戻し、それを変更する:次のコマンドを使用して

id count a b c age 
1 1  apple 2 3 50 
1 1  orange 3 2 50 
1 1  beer 2 1 50 
1 2  orange 3 2 50 
1 2  apple 2 2 50 
1 2  beer 2 1 50 
2 1  pear 3 2 45 
2 1  apple 2 2 45 
2 1  orange 2 2 45 

私が試してみましたリシェイプが、私はtimevarと時代に対処する場所の面で混乱してしまいます。

l <- reshape(df, 
      varying = df[,3:11], 
      v.names = c("a","b","c"), 
      timevar = "choice", 
      times = c("a","b","c"), 
      direction = "long") 

上記のコマンドを使用すると、私は望む結果が得られません。本当に助けていただければ幸いです!

答えて

2

reshape機能を使用するには、あなただけのさまざまな引数を調整する必要があります。これは、リストすることができ、あなたは、リスト内のベクトルとして一緒に同じ列を占めるようになる変数を入れたい:

reshape(df, 
     idvar=c("id", "count", "age"), 
     varying = list(c(3,6,9), c(4,7,10), c(5,8,11)), 
     timevar="time", 
     v.names=c("a", "b", "c"), 
     direction = "long") 

これは私がまた私としてidvarsに追加

  id count age time  a b c 
1.1.50.1 1  1 50 1 apple 2 3 
1.2.50.1 1  2 50 1 orange 3 2 
2.1.45.1 2  1 45 1 pear 3 2 
1.1.50.2 1  1 50 2 orange 3 2 
1.2.50.2 1  2 50 2 apple 2 2 
2.1.45.2 2  1 45 2 apple 2 2 
1.1.50.3 1  1 50 3 beer 2 1 
1.2.50.3 1  2 50 3 beer 2 1 
2.1.45.3 2  1 45 3 orange 2 2 

を返します。これは通常、他の人や古いコードを読み直すのに適していると思います。

データ

df <- read.table(header=T, text="id count a1  b1 c1 a2  b2 c2 a3 b3 c3 age 
1 1  apple 2 3 orange 3 2 beer 2 1 50 
1 2  orange 3 2 apple 2 2 beer 2 1 50 
2 1  pear 3 2 apple 2 2 orange 2 2 45") 
+0

を使用することができ、それは.subset(X、J)でエラーを言う:無効な添字型 'list' の – lll

+0

私はまだ始まったばかり新しいRセッションを作成し、エラーなしで追加したデータでコードを実行しました。 – lmo

+1

さまざまなグループや名前を手動で指定する必要はありません。 'reshape()'は 'reshape(dat、idvar = c(" id "、" count "、" age ")、direction =" long "、varying = 3:11、sep =" ")' 'sep =" "は各' a/b/c'と '1/2/3'が何も' '" 'timevar'と' v.names'が推論されます。 – thelatemail

4

使用data.tableパッケージからmelt機能:

library(data.table) 
setDT(df) 
melt(df, id.vars = c('id', 'count', 'age'), 
     measure = patterns('a\\d', 'b\\d', 'c\\d'), 
     # this needs to be regular expression to group `a1, a2, a3` etc together and 
     # the `\\d` is necessary because you have an age variable in the column. 
     value.name = c('a', 'b', 'c'))[, variable := NULL][order(id, count, -age)] 

# id count age  a b c 
# 1: 1  1 50 apple 2 3 
# 2: 1  1 50 orange 3 2 
# 3: 1  1 50 beer 2 1 
# 4: 1  2 50 orange 3 2 
# 5: 1  2 50 apple 2 2 
# 6: 1  2 50 beer 2 1 
# 7: 2  1 45 pear 3 2 
# 8: 2  1 45 apple 2 2 
# 9: 2  1 45 orange 2 2 
3

私がしようとしたとき、私たちはdplyr/tidyr

library(dplyr) 
library(tidyr) 
gather(df1, Var, Val, a1:c3) %>% 
     extract(Var, into = c("Var1", "Var2"), "(.)(.)") %>% 
     spread(Var1, Val) %>% 
     select(-Var2) 
# id count age  a b c 
#1 1  1 50 apple 2 3 
#2 1  1 50 orange 3 2 
#3 1  1 50 beer 2 1 
#4 1  2 50 orange 3 2 
#5 1  2 50 apple 2 2 
#6 1  2 50 beer 2 1 
#7 2  1 45 pear 3 2 
#8 2  1 45 apple 2 2 
#9 2  1 45 orange 2 2 
関連する問題