I have a dataset with microRNAs in 8 different groups. I need to transform this data frame into a binary matrix using R. The number of microRNAs is different in the groups and I would like to make the groups in the row and have the microRNAs on the columns. Here is part of the data:
Group1 Group2 Group3 Group4
miR-133a miR-133b miR-456 miR777
miR-777 miR138 miR-564 miR-878
miR-878 miR-777 miR978
miR-878
miR-978
Output expected:
Groups miR-133a miR-133b miR-456 miR-777.....
Group1 1 0 0 1
Group2 0 1 0 0
.
.
.
I tried to use this code:
im <- which(arr.ind=T,Dat!='');
u <- unique(Dat[im[order(im[,'row'],im[,'col']),]]);
res <- matrix(0L,nrow(Dat),length(u),dimnames=list(NULL,u));
res[cbind(im[,'row'],match(Dat[im],u))] <- 1L;
res
But it is giving me a lot of rows. Can anyone help me with that?