Note that there are some explanatory texts on larger screens.

plurals
  1. POMelting/casting my data into shape
    primarykey
    data
    text
    <p>I have a table that looks like this:</p> <p><img src="https://i.stack.imgur.com/Lf92A.png" alt="enter image description here"></p> <p>And I need it to look like this, where net=gross-tare:</p> <p><img src="https://i.stack.imgur.com/19u4V.png" alt="enter image description here"></p> <p>How do I do this? </p> <hr> <p>I started by melting the data, then casting as columns, and then creating new columns for the net readings. </p> <pre><code> df_m &lt;- melt(df, id = 1:3) df_c &lt;- cast(df_m, ... ~ variable + type) df_c$wr_net &lt;- df_c$wr_gross - df_c$wr_tare df_c$wc_net &lt;- df_c$wc_gross - df_c$wc_tare df_c$tsa_net &lt;- df_c$tsa_gross - df_c$tsa_tare </code></pre> <p>Which gives</p> <p><img src="https://i.stack.imgur.com/vYsXA.png" alt="enter image description here"></p> <p>But now I can't figure out how to melt this table to get the dataframe to look the way I need with a column for 'type' with values 'gross' and 'tare' and 'net'. </p> <p>Is there an easier way? Am I barking up the wrong tree with melt/cast?</p> <hr> <p>You can reproduce a small sample of my data using this...</p> <pre><code> df &lt;- 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)) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload