Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>reshape2</code>:</p> <pre><code># Thanks to Ista for helping with direct naming using "variable.name" df.m &lt;- melt(df, id.var = c("ID", "Test", "Year"), variable.name = "Time") df.m &lt;- transform(df.m, Test = paste0("Test", Test)) dcast(df.m, ID + Year + Time ~ Test, value.var = "value") </code></pre> <hr> <h3>Update: Using data.table melt/cast from versions >= 1.9.0:</h3> <p><code>data.table</code> from versions 1.9.0 imports <code>reshape2</code> package and implements fast <code>melt</code> and <code>dcast</code> methods in C for data.tables. A comparison of speed on bigger data is shown below.</p> <p>For more info regarding NEWS, go <a href="https://stackoverflow.com/a/6913151/559784"><strong>here</strong></a>.</p> <pre><code>require(data.table) ## ver. &gt;=1.9.0 require(reshape2) dt &lt;- as.data.table(df, key=c("ID", "Test", "Year")) dt.m &lt;- melt(dt, id.var = c("ID", "Test", "Year"), variable.name = "Time") dt.m[, Test := paste0("Test", Test)] dcast.data.table(dt.m, ID + Year + Time ~ Test, value.var = "value") </code></pre> <p>At the moment, you'll have to write <code>dcast.data.table</code> explicitly as it's not a S3 generic in <code>reshape2</code> yet.</p> <hr> <h3>Benchmarking on bigger data:</h3> <pre><code># generate data: set.seed(45L) DT &lt;- data.table(ID = sample(1e2, 1e7, TRUE), Test = sample(1e3, 1e7, TRUE), Year = sample(2008:2014, 1e7,TRUE), Fall = sample(50, 1e7, TRUE), Spring = sample(50, 1e7,TRUE), Winter = sample(50, 1e7, TRUE)) DF &lt;- as.data.frame(DT) </code></pre> <h3>reshape2 timings:</h3> <pre><code>reshape2_melt &lt;- function(df) { df.m &lt;- melt(df, id.var = c("ID", "Test", "Year"), variable.name = "Time") } # min. of three consecutive runs system.time(df.m &lt;- reshape2_melt(DF)) # user system elapsed # 43.319 4.909 48.932 df.m &lt;- transform(df.m, Test = paste0("Test", Test)) reshape2_cast &lt;- function(df) { dcast(df.m, ID + Year + Time ~ Test, value.var = "value") } # min. of three consecutive runs system.time(reshape2_cast(df.m)) # user system elapsed # 57.728 9.712 69.573 </code></pre> <h3>data.table timings:</h3> <pre><code>DT_melt &lt;- function(dt) { dt.m &lt;- melt(dt, id.var = c("ID", "Test", "Year"), variable.name = "Time") } # min. of three consecutive runs system.time(dt.m &lt;- reshape2_melt(DT)) # user system elapsed # 0.276 0.001 0.279 dt.m[, Test := paste0("Test", Test)] DT_cast &lt;- function(dt) { dcast.data.table(dt.m, ID + Year + Time ~ Test, value.var = "value") } # min. of three consecutive runs system.time(DT_cast(dt.m)) # user system elapsed # 12.732 0.825 14.006 </code></pre> <p><code>melt.data.table</code> is <strong>~175x faster</strong> than <code>reshape2:::melt</code> and <code>dcast.data.table</code> is <strong>~5x</strong> than <code>reshape2:::dcast</code>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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