-1

I have a data like this:

1 233
1 333
1 455
1 345
2 543
2 433
2 344
2 400
3 444
3 111
3 000
3 432

I want to change it to this new dataset like this:

1    2    3
233  543  444
333  433  111
455  344  000
345  400  432

How can I do this in R? do any body knows a script for this. please note that my real data is very larger and number of rows is large.

Frank
  • 66,179
  • 8
  • 96
  • 180
zara
  • 1,048
  • 3
  • 9
  • 19
  • Anyway, if you create a new column `df$id = 1:4`, then it looks like you are in the scope of this question: http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix and could do `library(reshape2); acast(df, id~V1, value.var="V2")` – Frank Sep 14 '15 at 22:09
  • @Frank : sorry for that. I try to explain better. I have several value for each level. for example for level 1 I have four values(233,333,455,345). and each value with its level is settled in one row. I want to put all values of a specific level in one column instead of having several rows with one value of each level. is that clear now? – zara Sep 14 '15 at 22:11
  • 1
    Perhaps `do.call(cbind, split(df[-1], df[[1]]))` – Rich Scriven Sep 14 '15 at 22:15
  • Ok. Better to add the explanation into the question itself. I think the downvote probably came because you told Matthew that your real data significantly differs from the example here (and not just in size). Best to make an example that captures the important features of your case. – Frank Sep 14 '15 at 22:24

1 Answers1

1

With x as your data frame with columns V1 and V2, you can add indices counting the elements in each level:

> x$V0 <- ave(x$V1, x$V1, FUN=seq_along)
> x
   V1  V2 V0
1   1 233  1
2   1 333  2
3   1 455  3
4   1 345  4
5   2 543  1
6   2 433  2
7   2 344  3
8   2 400  4
9   3 444  1
10  3 111  2
11  3   0  3
12  3 432  4

Now apply reshape:

> reshape(x, direction='wide', timevar='V1', idvar='V0')
  V0 V2.1 V2.2 V2.3
1  1  233  543  444
2  2  333  433  111
3  3  455  344    0
4  4  345  400  432

x:

structure(list(V1 = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L), V2 = c(233L, 333L, 455L, 345L, 543L, 433L, 344L, 400L, 
444L, 111L, 0L, 432L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-12L))
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112