Hi, sapply(colnames(m)[-ncol(m)],function(i) {x1<-cbind(m[,i],m[,ncol(m)]); length(which(x1[,1]!=0 & x1[,2]!=0))}) #A B C #2 1 2 A.K.
----- Original Message ----- From: Hermann Norpois <hnorp...@gmail.com> To: r-help <r-help@r-project.org> Cc: Sent: Friday, June 14, 2013 3:51 PM Subject: [R] combination of columns in a matrix Hello, I have a matrix m and I want to know how often does 1 (or !0) simultanously appear in A and REF, B and REF, C and REF. So actually I wish to automate following expression: > length (which (m[,1]!=0&m[,4]!=0)) [1] 2 > length (which (m[,2]!=0&m[,4]!=0)) [1] 1 Thanks Hermann > m A B C REF r1 1 0 0 1 r2 1 1 0 0 r3 0 0 1 1 r4 0 0 1 1 r5 1 1 0 1 > dput (m) structure(c(1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1), .Dim = c(5L, 4L), .Dimnames = list(c("r1", "r2", "r3", "r4", "r5"), c("A", "B", "C", "REF"))) [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.