Firstly I'm pretty sure this has been answered before but the search terms seem difficult to hit, apologies if there is a duplicate out there.
Say I have a vector of factors:
all <- factor(letters)
And I've gone on to use all combinations of those factor levels as part of a modelling pipeline:
combos <- t(combn(as.character(all), 5))
head(combos)
# [,1] [,2] [,3] [,4] [,5]
# [1,] "a" "b" "c" "d" "e"
# [2,] "a" "b" "c" "d" "f"
# [3,] "a" "b" "c" "d" "g"
# ...
My question is: How can I convert this second matrix to one showing presence/absence of all levels, like:
a b c d e f g ...
[1,] 1 1 1 1 1 0 0 ...
[2,] 1 1 1 1 0 1 0 ...
[3,] 1 1 1 1 0 0 1 ...
...
In terms of what I've tried, my first thought was a row-wise application of ifelse
using apply
, but I haven't been able to put anything workable together. Any smart way of doing this?