Here is a base R option using table + paste
as.data.frame(
table(
trimws(
do.call(
paste,
as.data.frame(
ifelse(df[-1] > 0,
names(df[-1])[col(df[-1])],
""
)
)
)
)
)
)
which gives
Var1 Freq
1 apple 5
2 apple orange 2
3 apple banana orange 1
4 banana 3
5 orange 2
Or
as.data.frame(
table(
apply(
as.data.frame(ifelse(df[-1] > 0, names(df[-1])[col(df[-1])], NA)),
1,
function(x) toString(na.omit(x))
)
)
)
which gives
Var1 Freq
1 apple 5
2 apple, banana, orange 1
3 apple, orange 2
4 banana 3
5 orange 2
Data
df <- data.frame(
student = 1:13,
apple = c(0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0),
banana = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1),
orange = c(0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0)
)