答案 0 :(得分:3)
使用dplyr
和tidyr
:
DF <- data.frame(ID = 1:3,
A = c(0, 1, 1),
B = c(1, 0, 0),
C = c(0, 0, 0),
D = c(1, 0, 1),
E = c(0, 1, 0))
library(dplyr)
library(tidyr)
out1 <-
gather(DF, letter, bit, A:E) %>%
filter(bit == 1) %>%
arrange(ID, letter)
out2 <-
out1 %>%
group_by(ID) %>%
summarise(output = paste0(letter, collapse = ","))