Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top