-2

I have a table as follows:

  Outcome   System  k_pval  k_exactpval k
  A          R       num    num        num
  B          S       num    num        num
  C          T       num    num        num
  A          X       num    num        num
  B          Y       num    num        num

How do I do across tabulation so that System appears in the columns, Outcome in the rows, and then for each system/outcome pair there are 3 columns (k, k_pval, k_exact_pval).

So for instance, you would have

     A      A            A             B       B        B
R   k_1    k_p_val1  k_exact_pval1     k_2    k_pval2   k_exact_pval2  
S   k_3    k_p_val3  k_exact_pval3     k_4    k_pval4   k_exact_pval4    

Thanks!

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
user1357015
  • 11,168
  • 22
  • 66
  • 111

1 Answers1

1

For reshaping your data, the reshape2 package is very helpful. I think this will accomplish what you are after.

dd <- data.frame(
    Outcome = c("A", "B", "C", "A", "B"), 
    System = c("R", "S", "T", "X", "Y"),
    k_pval = c(0.02, 0.04, 0.01, 0.1, 0.22), 
    k_exactpval = c(0.05, 0.02, 0.01, 0.3, 0.1), 
        k = c(4, 5, 3, 3, 1)
)

mm<-melt(dd, id.var=c("Outcome","System"))
dcast(mm, System~variable+Outcome)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • oOo, this is close. I need something a little different.. I've reworked my question to be more explicit.. – user1357015 May 08 '14 at 17:55
  • 1
    A valid R table cannot have duplicate column names (ie, you cannot have three columns named "A"). Please re-think what result you acually need. If it's just that you want all the A terms first, just switch the order to `dcast(mm, System~Outcome+variable)` – MrFlick May 08 '14 at 17:59
  • 1
    +1. Considering the setup of the data, this is more directly a base R `reshape` problem than a "reshape2" one: `reshape(dd, direction = "wide", idvar="System", timevar="Outcome")` – A5C1D2H2I1M1N2O1R2T1 May 08 '14 at 18:46