我想要投射此数据框
ID GROUP CHAR_VALUE
A001 AAA
A001 BBB B01
A001 DDD X99
A002 AAA A50
A002 FFF FFF
A003
A004 FFF F01
...
进入这样的对象:
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
...
使用此声明
library(reshape)
cast(tbl, ID ~ GROUP, value = "CHAR_VALUE")
我得到了这个二元结果
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
...
请注意,我有一百万条记录,所以表现很重要。
与整数相似的问题: How to use cast on a data frame?
更新: 它抛出以下错误:
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