1

I would need your help with R to cast this data:

    STATUS     SCORE  JOB.ID 
1   STATUS1    99     JOB1
2   STATUS1    99     JOB2 
3   STATUS1    99     JOB3
4   STATUS1    99     JOB4
5   STATUS2    36     JOB5
6   STATUS2    36     JOB6
7   STATUS2    49     JOB7
8   STATUS2    58     JOB8
9   STATUS2    64     JOB9

to this wide form:

    STATUS1   STATUS2
1   JOB1      JOB6
2   JOB2      JOB7
3   JOB3      JOB8
4   JOB4      JOB9
5   JOB5      <NA> 

I do not need to compute a value, I just need to make lists. Ideally the score would be used to sort the resulting lists. The columns length are uneven. I am unable to find an elegant answer elsewhere. Thank you.

  • I'm not sure it makes sense to put that into a data frame. Keeping them as separate lists... `list_em <- by(df$JOB.ID,df$STATUS,unique)` assuming you have `factor`s. – Frank May 14 '13 at 16:53

1 Answers1

1

If you really do want them all in a single data frame, you could do something like this:

library(reshape2)
library(plyr)
dat <- read.table(text = "    STATUS     SCORE  JOB.ID 
 1   STATUS1    99     JOB1
 2   STATUS1    99     JOB2 
 3   STATUS1    99     JOB3
 4   STATUS1    99     JOB4
 5   STATUS2    36     JOB5
 6   STATUS2    36     JOB6
 7   STATUS2    49     JOB7
 8   STATUS2    58     JOB8
 9   STATUS2    64     JOB9",header = TRUE,sep = "")
> dat <- ddply(dat,.(STATUS),transform,ind = seq_along(STATUS))
> dcast(dat,ind~STATUS,fill = NA,value.var = "JOB.ID")
  ind STATUS1 STATUS2
1   1    JOB1    JOB5
2   2    JOB2    JOB6
3   3    JOB3    JOB7
4   4    JOB4    JOB8
5   5    <NA>    JOB9
joran
  • 169,992
  • 32
  • 429
  • 468
  • Amazing thank you. Extra point for correcting my typo putting JOB5 in the wrong column. – Michael Robert May 14 '13 at 17:03
  • 1
    @MichaelRobert Glad I could help. If this answer helped you, please give back to the site that solved your problem by learning about voting and accepting answers [here](http://stackoverflow.com/faq#howtoask). – joran May 14 '13 at 17:13