0

I want to cast this data frame

ID    GROUP CHAR_VALUE
A001  AAA    
A001  BBB   B01
A001  DDD   X99
A002  AAA   A50
A002  FFF   FFF
A003    
A004  FFF   F01
...

Into an object like this:

ID    AAA    BBB    DDD    FFF    ...
A001  N/A    B01    X99    N/A    ...
A002  A50    N/A    N/A    FFF
A003  N/A    N/A    N/A    N/A
A004  N/A    N/A    N/A    F01
...

With this statement

library(reshape)  
cast(tbl, ID ~ GROUP, value = "CHAR_VALUE")

I get this binary outcome

ID    AAA    BBB    DDD    FFF    ...
A001  1      1      1      0      ...
A002  1      0      0      1
A003  0      0      0      0
A004  0      0      0      1
...

Please note that i have some million records, so performance is important.

Similar question with integers: How to use cast on a data frame?

Update: Its throwing the following error:

Using CHAR_VALUE as value column.
Use the value argument to cast to override this choice Aggregation requires fun.
aggregate: length used as default Error in FUN(X[[i]], ...):
2 arguments passed to 'length' which requires 1
Community
  • 1
  • 1
  • I was able to get the expected output with `dcast` from `reshape2` i.e. `dcast(tbl, ID ~GROUP, value.var = "CHAR_VALUE")` – akrun Aug 13 '16 at 12:44
  • Actually, I was able to get the expected output using `cast` from `reshape`. – aichao Aug 13 '16 at 12:52
  • Its throwing this error: `Using CHAR_VALUE as value column. Use the value argument to cast to override this choice Aggregation requires fun.aggregate: length used as default Error in FUN(X[[i]], ...) : 2 arguments passed to 'length' which requires 1` – Mathias Quetschlich Aug 13 '16 at 18:56
  • Thanks Dirty Sock Sniffer. Following code does the job: `reshape(test, idvar= "ID", timevar = "GROUP", direction = "wide")` Still wondering why `cast` does not work though... – Mathias Quetschlich Aug 13 '16 at 19:12

0 Answers0