[R] Extract pairs (rowname, columname) from a matrix where value is 0

David Winsemius dwinsemius at comcast.net
Thu Jul 16 06:27:50 CEST 2009


On Jul 15, 2009, at 7:15 PM, Alberto Lora M wrote:

> Dear sir,
>
> I have a matrix like
>
> a<-matrix(c(0,2,0,4,0,6,5,8,0),nrow=3)
> colnames(a)<-c("F1","F2","F3")
> rownames(a)<-c("A1","A2","A3")
> a
>
>   F1 F2 F3
> A1  0  4  5
> A2  2  0  8
> A3  0  6  0
>
> I want to extract all pairs (rownames, columnames) from which the  
> value in
> the matrix is 0
>
> The result should be something like this
>
> A1, F1
> A2, F2
> A3, F1
> A3, F3
>
> how it is possible?

 > rep(rownames(a),3)[which(a==0)]
[1] "A1" "A3" "A2" "A3"
 > c(rep("F1",3),rep("F2",3),rep("F3",3))[which(a==0)]
[1] "F1" "F1" "F2" "F3"

Or bundled:

 > drc <-data.frame(rownms =rep(rownames(a),3)[which(a==0)],  
colnms=c(rep("F1",3),rep("F2",3),rep("F3",3))[which(a==0)] )
 > drc
   rownms colnms
1     A1     F1
2     A3     F1
3     A2     F2
4     A3     F3

>
> thanks for your help....
>
> Best Regards
>
> Alberto
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list