Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Binary Matrix in R

Tags:

r

matrix

I've got 3 training sets having 5 different events: list(c('e1','e3','e4'),c('e2','e4'),c('e1','e3','e5'))

I don't know how many events will be there in my list. So I need to count them also. Here I have shown e1 to e5 events as an example. There can be m number of events where I need to find m also. How to find m? In general, how to create m-by-n binary matrix in R where m-total number of events, n-number of training sets? event-IDs might be random, not necessarily of 'eN' type like 'MX050331','IIF02002' etc.

like image 975
anu Avatar asked Jan 19 '26 10:01

anu


1 Answers

# per the OP's second edit
# now assumes there are no events that haven't been observed at least once
tr <- list(c('e1','e3','e4'),c('e2','e4'),c('e1','e3','e5'))
e <- unique(unlist(tr))
sapply(tr, function(x) e %in% x)
like image 163
Hong Ooi Avatar answered Jan 21 '26 01:01

Hong Ooi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!