Alberto Lora M
2009-Jul-15 23:15 UTC
[R] Extract pairs (rowname, columname) from a matrix where value is 0
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? thanks for your help.... Best Regards Alberto [[alternative HTML version deleted]]
David Winsemius
2009-Jul-16 04:27 UTC
[R] Extract pairs (rowname, columname) from a matrix where value is 0
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
Jorge Ivan Velez
2009-Jul-16 04:40 UTC
[R] Extract pairs (rowname, columname) from a matrix where value is 0
Dear Alberto, Try this: # colnames and rownames cn <- colnames(a) rn <- rownames(a) # the index ind <- which(a==0, arr = TRUE) # the result data.frame( res = apply(ind, 1, function(x) paste(rn[x[1]],",", cn[x[2]], sep ="") ) ) # res # 1 A1,F1 # 2 A3,F1 # 3 A2,F2 # 4 A3,F3 See ?which, ?apply and ?paste for more information. HTH, Jorge On Wed, Jul 15, 2009 at 7:15 PM, Alberto Lora M <albertoloram@gmail.com>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? > > thanks for your help.... > > Best Regards > > Alberto > > [[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. >[[alternative HTML version deleted]]
Gabor Grothendieck
2009-Jul-16 05:03 UTC
[R] Extract pairs (rowname, columname) from a matrix where value is 0
Here is a variation on the solution below (first line is the same but second differs):> ind <- which(a == 0, arr = TRUE) > mapply("[", dimnames(a), as.data.frame(ind))[,1] [,2] [1,] "A1" "F1" [2,] "A3" "F1" [3,] "A2" "F2" [4,] "A3" "F3" On Thu, Jul 16, 2009 at 12:40 AM, Jorge Ivan Velez<jorgeivanvelez at gmail.com> wrote:> Dear Alberto, > Try this: > > # colnames and rownames > cn <- colnames(a) > rn <- rownames(a) > > # the index > ind <- which(a==0, arr = TRUE) > > # the result > data.frame( res = apply(ind, 1, function(x) > ? ? ? ? ? ? ? ? ? ? ? ?paste(rn[x[1]],",", cn[x[2]], sep ="") > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?) > ? ? ? ? ? ?) > > # ? res > # 1 A1,F1 > # 2 A3,F1 > # 3 A2,F2 > # 4 A3,F3 > > See ?which, ?apply and ?paste for more information. > > HTH, > > Jorge > > > On Wed, Jul 15, 2009 at 7:15 PM, Alberto Lora M <albertoloram at gmail.com>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? >> >> 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. >> > > ? ? ? ?[[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. >
Alberto Lora M
2009-Jul-16 20:23 UTC
[R] Extract pairs (rowname, columname) from a matrix where value is 0
Dear sir, I have 2 questions: Question 1: suppose that we have the following: 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 ind <- which(a == 0, arr = TRUE) mapply("[", dimnames(a), as.data.frame(ind)) I want to add a column to the result. This Column added depends on the 2 first column value of the row. The result should be something like this: [,1] [,2] [,3] [1,] "A1" "F1" "A1 excludes F1" [2,] "A3" "F1" "A3 excludes F1" [3,] "A2" "F2" "A2 excludes F2" [4,] "A3" "F3" "A3 excludes F3" Question 2a) I am also working with arules package and I have the following problem let suppose the matrix b like: b<-matrix(c(1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1,1),nrow=6) rownames(b)=c("T1", "T2", "T3", "T4", "T5", "T6") colnames(b)=c("It1", "It2", "It3", "It4") bt<-as(b, "transactions") rules<-apriori(bt, parameter = list(maxlen=2)) k<-inspect(rules) k ##we obtain lhs rhs support confidence lift 1 {} => {It4} 1.0000000 1 1.0 2 {} => {It1} 1.0000000 1 1.0 3 {It3} => {It2} 0.5000000 1 1.5 4 {It3} => {It4} 0.5000000 1 1.0 5 {It3} => {It1} 0.5000000 1 1.0 6 {It2} => {It4} 0.6666667 1 1.0 7 {It2} => {It1} 0.6666667 1 1.0 8 {It4} => {It1} 1.0000000 1 1.0 9 {It1} => {It4} 1.0000000 1 1.0 WRITE(rules) ##we obtain "rules" "support" "confidence" "lift" "1" "{} => {It4}" 1 1 1 "2" "{} => {It1}" 1 1 1 "3" "{It3} => {It2}" 0.5 1 1.5 "4" "{It3} => {It4}" 0.5 1 1 "5" "{It3} => {It1}" 0.5 1 1 "6" "{It2} => {It4}" 0.666666666666667 1 1 "7" "{It2} => {It1}" 0.666666666666667 1 1 "8" "{It4} => {It1}" 1 1 1 "9" "{It1} => {It4}" 1 1 1 I want to convert this in a matrix or data frame and the result should be something like this ##we obtain rules support confidence lift 1 {}=>{It4} 1.0000000 1 1.0 2 {}=>{It1} 1.0000000 1 1.0 3 {It3}=>{It2} 0.5000000 1 1.5 4 {It3}=>{It4} 0.5000000 1 1.0 5 {It3}=>{It1} 0.5000000 1 1.0 6 {It2}=>{It4} 0.6666667 1 1.0 7 {It2}=>{It1} 0.6666667 1 1.0 8 {It4}=>{It1} 1.0000000 1 1.0 9 {It1}=>{It4} 1.0000000 1 1.0 In another hand is it possible to obtain all the rules where lHS<=>rhs?. In our last example that means to obtain lhs rhs support confidence lift 8 {It4} => {It1} 1.0000000 1 1.0 9 {It1} => {It4} 1.0000000 1 1.0 Thanks again Alberto [[alternative HTML version deleted]]