Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As @Andrie said, the first step is melting the data with your given columns (ID and gender). Your problem, as you say, is identifying what columns then "go together". Here is one approach, originally encoding that information in column names, and then pulling it out from there.</p> <p>First some dummy data</p> <pre><code>dat &lt;- data.frame(ID=c("ID_1", "ID_2"), Gender=c("m","f"), Test1.A = "A1", Test2.A = "A2", Test3.A = "A3", Test1.B = "B1", Test2.B = "B2", Test3.B = "B3", stringsAsFactors=FALSE) </code></pre> <p>Note that I've named the columns with a name that systematically indicates which test and which group it is part of.</p> <pre><code>&gt; dat ID Gender Test1.A Test2.A Test3.A Test1.B Test2.B Test3.B 1 ID_1 m A1 A2 A3 B1 B2 B3 2 ID_2 f A1 A2 A3 B1 B2 B3 </code></pre> <p>Using the <code>reshape2</code> package</p> <pre><code>library("reshape2") </code></pre> <p>Melt the data, and then take the <code>variable</code> column which has two pieces of information in it (test and group), and split those two bits of info into two separate columns.</p> <pre><code>dat.m &lt;- melt(dat, id.vars=c("ID", "Gender")) dat.m &lt;- cbind(dat.m, colsplit(dat.m$variable, "\\.", names=c("Test", "Group"))) </code></pre> <p>Now it is easy to cast since the test and the group are separate.</p> <pre><code>dcast(dat.m, ID+Gender+Group~Test) </code></pre> <p>Which gives</p> <pre><code>&gt; dcast(dat.m, ID+Gender+Group~Test) ID Gender Group Test1 Test2 Test3 1 ID_1 m A A1 A2 A3 2 ID_1 m B B1 B2 B3 3 ID_2 f A A1 A2 A3 4 ID_2 f B B1 B2 B3 </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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