イントロ
私はまだRの専門家ではないので、恥ずかしいと思うかもしれない別の質問を許してください。私がstackoverflowで尋ねた別の質問では、xtsオブジェクトの不規則な毎日のデータを
apply.weekly()関数によって毎週の値に集約する方法についていくつかの非常に役立つコメントを受け取りました。残念ながら、、、、または関数と連携して機能するカテゴリごとに分割できる関数は見つかりませんtapply()でした。ddply()by()aggregate()apply.weekly()
私のデータ
これは私のサンプルデータセットです。私はすでに他の質問に投稿しました。説明のために、私は自由にそれをここに投稿します:
example <- as.data.frame(structure(c(" 1", " 2", " 1", " 2", " 1", " 1", " 2", " 1", " 2",
" 1", " 2", " 3", " 1", " 1", " 2", " 2", " 3", " 1", " 2", " 2",
" 1", " 2", " 1", " 1", " 2", NA, " 2", NA, NA, " 1", " 3", " 1",
" 3", " 3", " 2", " 3", " 3", " 3", " 2", " 2", " 2", " 3", " 3",
" 3", " 2", " 2", " 3", " 3", " 3", " 3", " 1", " 2", " 1", " 2",
" 2", " 1", " 2", " 1", " 2", " 2", " 2", " 3", " 1", " 1", " 2",
" 2", " 3", " 3", " 2", " 2", " 1", " 2", " 1", " 1", " 2", NA,
" 2", NA, NA, " 1", " 3", " 2", " 3", " 2", " 0", " 3", " 3",
" 3", " 2", " 0", " 2", " 3", " 3", " 3", " 0", " 2", " 2", " 3",
" 3", " 0", "12", " 5", " 9", "14", " 5", "tra", "tra", "man",
"inf", "agc", "07-2011", "07-2011", "07-2011", "07-2011", "07-2011"
), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", class = c("xts",
"zoo"), .indexFORMAT = "%U-%Y", index = structure(c(1297642226,
1297672737, 1297741204, 1297748893, 1297749513), tzone = "", tclass = c("POSIXlt",
"POSIXt")), .Dim = c(5L, 23L), .Dimnames = list(NULL, c("rev_sit",
"prof_sit", "emp_nr_sit", "inv_sit", "ord_home_sit", "ord_abr_sit",
"emp_cost_sit", "usage_cost_sit", "tax_cost_sit", "gov_cost_sit",
"rev_exp", "prof_exp", "emp_nr_exp", "inv_exp", "ord_home_exp",
"ord_abr_exp", "emp_cost_exp", "usage_cost_exp", "tax_cost_exp",
"gov_cost_exp", "land", "nace", "index"))))
列
"rev_sit"、 "prof_sit"、 "emp_nr_sit"、 "inv_sit"、 "ord_home_sit"、 "ord_abr_sit"、 "emp_cost_sit"、 "usage_cost_sit"、 "tax_cost_sit"、 "gov_cost_sit"、 "rev_exp" _ " "、" inv_exp "、" ord_home_exp "、" ord_abr_exp "、" emp_cost_exp "、" usage_cost_exp "、" tax_cost_exp "、" gov_cost_exp "、
調査の質問を参照してください。「1」、「2」、「3」の3つの回答可能性コードがありました。
列
「土地」、「ネイス」
それぞれ16と8の固有の要因を持つカテゴリです。
私の目標 私の目標は、「nace」と「land」のカテゴリ要素の組み合わせごとに、週ごとに「1」、「2」、および「3」の発生をカウントすることです。私のアイデアは、事前に回答の可能性{1,2,3}ごとにバイナリベクトルを作成し(example_1、example_2、example_2)、次のようなものを適用することでした。
apply.weekly(example_1, function(d){ddply(d,list(example$nace,example$land),sum)})
ddplyしかし、これは、、などaggregateでも機能しません。by
私の目標
私の専門的でない回避策は、最初は時系列を作成することではなくexample$date、指定された時間列が毎週としてコード化された日付ベクトルを作成することでした%V。
tapply(example_1[,5], list(example$date,example$nace,example$land),sum)
もちろん、上記の20の質問のすべてに対して行う必要があります。次に、たとえばexample_1を取得します。
week1、nace1.land1、nace1.land2、nace1.land3、...、nace1.land16、nace2.land1、..、nace8.land16 week2、nace1.land1、nace1.land2、nace1.land3、...、 nace1.land16、nace2.land1、..、nace8.land16 ... ... weekn、nace1.land1、nace1.land2、nace1.land3、...、nace1.land16、nace2.land1、..、nace8 .land16
2(example_2)と3(example_3)についても同じことを行う必要があります。これは、すべての16 * 8 * 3 * 20=7680列ですべてを生成するための20の質問のそれぞれについてです。この極端な、さらにこの方法では、製品は時系列ではないため、週ごとに正しく注文されません。
概要
したがって、誰かが私に教えたり、関数を関数apply.weekly()と組み合わせて、、、、などの種類の関数をtapply()使用する方法やddply()、上記のようなグループ化を実現するための他の方法を教えてもらえますか?すべてのヒントは本当にありがたいです。私はすでにRの実験をやめて、多くのことがはるかに直感的なスタタに戻ることなどを考えていることにとても不満を感じています...しかし、私を誤解しないでください:私は学びたいので助けてください!by()split()unstack()collapse()by()