次のようなテーブルがあります。
そして、net=gross-tare の場合、次のようにする必要があります。
どうすればいいですか?
まず、データを溶かして列としてキャストし、正味の読み取り値用に新しい列を作成しました。
df_m <- melt(df, id = 1:3)
df_c <- cast(df_m, ... ~ variable + type)
df_c$wr_net <- df_c$wr_gross - df_c$wr_tare
df_c$wc_net <- df_c$wc_gross - df_c$wc_tare
df_c$tsa_net <- df_c$tsa_gross - df_c$tsa_tare
どちらが与える
しかし今、このテーブルを溶かして、データフレームを「総」と「風袋」と「正味」の値を持つ「タイプ」の列で必要な方法で表示する方法を理解できません。
もっと簡単な方法はありますか?メルト/キャストで間違ったツリーを吠えていますか?
これを使用して、私のデータの小さなサンプルを再現できます...
df <- structure(list(train = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "AC0485n", class = "factor"),
position = c(1L, 1L, 2L, 2L, 3L, 3L), type = structure(c(2L,
1L, 2L, 1L, 2L, 1L), .Label = c("gross", "tare"), class = "factor"),
wids_raw = c(24.85, 146.2, 26.16, 135, 24.7, 135.1), wids_corr = c(26.15,
145.43, 27.44, 134.43, 26, 134.52), tsa = c(24.1, 139.2,
25, 133.6, 24, 131.1)), .Names = c("train", "position", "type",
"wr", "wc", "tsa"), class = "data.frame", row.names = c(NA,
-6L))