将数据融化/铸造为形状
|
我有一个看起来像这样的表:
我需要它看起来像这样,其中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
这使
但是现在我无法弄清楚如何融合此表以使数据框看起来像我需要的方式,即带有'type \'列的值具有'\ gross \'和\'tare \'和\'net \'。
有更容易的方法吗?我是否用融化/浇铸方法把错误的树吠叫?
您可以使用此方法复制我的数据的一小部分...
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))
没有找到相关结果
已邀请:
2 个回复
砷竣阿
返回,
编辑: 而且我认为,如果您真的想使用熔铸/浇铸,这会起作用:
浩挎