我有一个充满二进制变量的表,我想将其浓缩为分类变量。
非常简单地说,我有一个这样的数据框:
data <- data.frame(id=c(1,2,3,4,5,6,7,8,9), red=c("1","0","0","0","1","0","0","0","0"),blue=c("0","1","1","1","0","1","1","1","0"),yellow=c("0","0","0","0","0","0","0","0","1"))
data
id red blue yellow
1 1 1 0 0
2 2 0 1 0
3 3 0 1 0
4 4 0 1 0
5 5 1 0 0
6 6 0 1 0
7 7 0 1 0
8 8 0 1 0
9 9 0 0 1
我想回来的是:
id color
1 1 red
2 2 blue
3 3 blue
4 4 blue
5 5 red
6 6 blue
7 7 blue
8 8 blue
9 9 yellow
我希望有一个非常简单的答案。