【发布时间】:2015-07-04 10:41:17
【问题描述】:
我有一组编码为二项式的变量。
Pre VALUE_1 VALUE_2 VALUE_3 VALUE_4 VALUE_5 VALUE_6 VALUE_7 VALUE_8
1 1 0 0 0 0 0 1 0 0
2 1 0 0 0 0 1 0 0 0
3 1 0 0 0 0 1 0 0 0
4 1 0 0 0 0 1 0 0 0
我想将变量 (VALUE_1, VALUE_2...VALUE_8) 合并为一个有序因子,同时按原样保留列 (Pre),使数据看起来像这样:
Pre VALUE
1 1 VALUE_6
2 1 VALUE_5
3 1 VALUE_5
甚至更好:
Pre VALUE
1 1 6
2 1 5
3 1 5
我知道这存在:Recoding dummy variable to ordered factor
但是当我尝试该帖子中使用的代码时,我收到以下错误:
PA2$Factor = factor(apply(PA2, 1, function(x) which(x == 1)), labels = colnames(PA2))
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
任何帮助将不胜感激
【问题讨论】: