Dear useRs,i have two distance matrices A and B. The distances in A are linked with distances in B. What i want to do is the following...There are some points in A where distances are zero(those points are apart from usual diagonal of zero values).Whereas, in B the distances at those points are not necessarily zeros . i want "0" at those points in B where distance in A is zero. How can i do that??i hope my question is clearthanks indeed in advance,Eliza [[alternative HTML version deleted]]
Hello, Try the following. set.seed(27846) A <- matrix(sample(0:2, 16, TRUE), ncol = 4) B <- matrix(1:16, ncol = 4) zeros <- which(A == 0, arr.ind = TRUE) B[zeros] <- 0 Hope this helps, Rui Barradas Em 19-05-2013 22:05, eliza botto escreveu:> Dear useRs,i have two distance matrices A and B. The distances in A are linked with distances in B. What i want to do is the following...There are some points in A where distances are zero(those points are apart from usual diagonal of zero values).Whereas, in B the distances at those points are not necessarily zeros . i want "0" at those points in B where distance in A is zero. How can i do that??i hope my question is clearthanks indeed in advance,Eliza > [[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. >
May be this helps: ?set.seed(28) ?mat1<- matrix(sample(0:50,20,replace=TRUE),nrow=5) A<- dist(mat1) A[2:4]<- 0 set.seed(35) ?mat2<- matrix(sample(1:50,20,replace=TRUE),nrow=5) B<- dist(mat2) B[which(A==0)]<-0 ?B #???????? 1??????? 2??????? 3??????? 4 #2 49.07138?????????????????????????? #3? 0.00000 28.19574????????????????? #4? 0.00000 55.70458 33.25658???????? #5? 0.00000 41.97618 31.35283 24.55606 A.K. ----- Original Message ----- From: eliza botto <eliza_botto at hotmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Sunday, May 19, 2013 5:05 PM Subject: [R] coping zeros from matrix to another Dear useRs,i have two distance matrices A and B. The distances in A are linked with distances in B. What i want to do is the following...There are some points in A where distances are zero(those points are apart from usual diagonal of zero values).Whereas, in B the distances at those points are not necessarily zeros . i want "0" at those points in B where distance in A is zero. How can i do that??i hope my question is clearthanks indeed in advance,Eliza ??? ??? ??? ? ??? ??? ? ??? [[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.
Thankyou very much Arun,it has even been simplified.. Eliza> Date: Sun, 19 May 2013 14:49:43 -0700 > From: smartpink111@yahoo.com > Subject: Re: [R] coping zeros from matrix to another > To: eliza_botto@hotmail.com > > #or > just > B[A==0]<-0 > > > > > ----- Original Message ----- > From: arun <smartpink111@yahoo.com> > To: eliza botto <eliza_botto@hotmail.com> > Cc: R help <r-help@r-project.org> > Sent: Sunday, May 19, 2013 5:48 PM > Subject: Re: [R] coping zeros from matrix to another > > May be this helps: > > set.seed(28) > mat1<- matrix(sample(0:50,20,replace=TRUE),nrow=5) > A<- dist(mat1) > A[2:4]<- 0 > set.seed(35) > mat2<- matrix(sample(1:50,20,replace=TRUE),nrow=5) > B<- dist(mat2) > B[which(A==0)]<-0 > B > # 1 2 3 4 > #2 49.07138 > #3 0.00000 28.19574 > #4 0.00000 55.70458 33.25658 > #5 0.00000 41.97618 31.35283 24.55606 > A.K. > > > ----- Original Message ----- > From: eliza botto <eliza_botto@hotmail.com> > To: "r-help@r-project.org" <r-help@r-project.org> > Cc: > Sent: Sunday, May 19, 2013 5:05 PM > Subject: [R] coping zeros from matrix to another > > Dear useRs,i have two distance matrices A and B. The distances in A are linked with distances in B. What i want to do is the following...There are some points in A where distances are zero(those points are apart from usual diagonal of zero values).Whereas, in B the distances at those points are not necessarily zeros . i want "0" at those points in B where distance in A is zero. How can i do that??i hope my question is clearthanks indeed in advance,Eliza > [[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]]