0

I have a dataframe with 3 columns (but this could be more depending on the number of files I brought in to the dataframe) and so I created a final column (a 4th column) with a numeric code to tell me where i want the data unstacked (each column contains 4 sets of data so I have coded them as such).

I want to unstack these three columns according to the sort column so that I now have 12 columns (3 columns x 4 sets).

I know this is probably so, so simple but I just can't get it.

Thanks in advance!

SNOYL
  • 3
  • 3
  • 2
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zx8754 May 28 '15 at 20:09
  • Looks like you have some lists in a dataframe. If you can show some example and expected result based on that, it would be easier – akrun May 28 '15 at 20:19
  • `df = do.call(cbind, split(df[,-ncol(df)], df$NameOfColumn4Here))` Something along these lines will work. – Vlo May 28 '15 at 20:20

1 Answers1

0

This can be done with reshape():

df <- cbind(matrix(1:24,8,dimnames=list(NULL,c(letters[1:3]))),code=rep(1:4,each=2));
df;
##      a  b  c code
## [1,] 1  9 17    1
## [2,] 2 10 18    1
## [3,] 3 11 19    2
## [4,] 4 12 20    2
## [5,] 5 13 21    3
## [6,] 6 14 22    3
## [7,] 7 15 23    4
## [8,] 8 16 24    4
reshape(transform(df,id=ave(code,code,FUN=seq_along)),dir='w',timevar='code')[-1];
##   a.1 b.1 c.1 a.2 b.2 c.2 a.3 b.3 c.3 a.4 b.4 c.4
## 1   1   9  17   3  11  19   5  13  21   7  15  23
## 2   2  10  18   4  12  20   6  14  22   8  16  24
bgoldst
  • 34,190
  • 6
  • 38
  • 64