Hello all, I'm having trouble creating an adjacency matrix. Basically, I need to turn the following distance matrix into an adjacency matrix based on whether values are >1.5 or not. If they are >1.5, then the returned value should be 0. If they are =<1.5, then the returned value should be 1. DistanceMatrix: A B C D E [1,] 0.000000 2.828427 1.581139 2.236068 2.000000 [2,] 2.828427 0.000000 1.581139 4.123106 2.000000 [3,] 1.581139 1.581139 0.000000 2.549510 2.121320 [4,] 2.236068 4.123106 2.549510 0.000000 4.123106 [5,] 2.000000 2.000000 2.121320 4.123106 0.000000 I'm trying to do this via the if statement, however I can't get it to work and it keeps returning an error message:> if(DistanceMatrix>1.5) {0} else {1}[1] 1 Warning message: In if (DistanceMatrix > 1.5) { : the condition has length > 1 and only the first element will be used I am brand new to R and have a very limited knowledge of functions and syntax. Maybe the if statement is not even the right way of tackling this problem.... Thanks in advance for your answers! L?a -- View this message in context: http://r.789695.n4.nabble.com/Trouble-creating-and-adjacency-matrix-tp3842106p3842106.html Sent from the R help mailing list archive at Nabble.com.
x<-rnorm(25,1.5,1) dim(x)=c(5,5) x y<-ifelse(c(x)>1.5,1,0) dim(y)<-dim(x) HTH, Daniel Spartina wrote:> > Hello all, > > I'm having trouble creating an adjacency matrix. > > Basically, I need to turn the following distance matrix into an adjacency > matrix based on whether values are >1.5 or not. If they are >1.5, then the > returned value should be 0. If they are =<1.5, then the returned value > should be 1. > > DistanceMatrix: > > A B C D E > [1,] 0.000000 2.828427 1.581139 2.236068 2.000000 > [2,] 2.828427 0.000000 1.581139 4.123106 2.000000 > [3,] 1.581139 1.581139 0.000000 2.549510 2.121320 > [4,] 2.236068 4.123106 2.549510 0.000000 4.123106 > [5,] 2.000000 2.000000 2.121320 4.123106 0.000000 > > I'm trying to do this via the if statement, however I can't get it to work > and it keeps returning an error message: > >> if(DistanceMatrix>1.5) {0} else {1} > [1] 1 > Warning message: > In if (DistanceMatrix > 1.5) { : > the condition has length > 1 and only the first element will be used > > > I am brand new to R and have a very limited knowledge of functions and > syntax. Maybe the if statement is not even the right way of tackling this > problem.... > > Thanks in advance for your answers! > > L?a >-- View this message in context: http://r.789695.n4.nabble.com/Trouble-creating-and-adjacency-matrix-tp3842106p3842449.html Sent from the R help mailing list archive at Nabble.com.
Hi Léa, Try ifelse(DistanceMatrix > 1.5, 0, 1) HTH, Jorge On Sun, Sep 25, 2011 at 5:07 PM, Spartina <> wrote:> Hello all, > > I'm having trouble creating an adjacency matrix. > > Basically, I need to turn the following distance matrix into an adjacency > matrix based on whether values are >1.5 or not. If they are >1.5, then the > returned value should be 0. If they are =<1.5, then the returned value > should be 1. > > DistanceMatrix: > > A B C D E > [1,] 0.000000 2.828427 1.581139 2.236068 2.000000 > [2,] 2.828427 0.000000 1.581139 4.123106 2.000000 > [3,] 1.581139 1.581139 0.000000 2.549510 2.121320 > [4,] 2.236068 4.123106 2.549510 0.000000 4.123106 > [5,] 2.000000 2.000000 2.121320 4.123106 0.000000 > > I'm trying to do this via the if statement, however I can't get it to work > and it keeps returning an error message: > > > if(DistanceMatrix>1.5) {0} else {1} > [1] 1 > Warning message: > In if (DistanceMatrix > 1.5) { : > the condition has length > 1 and only the first element will be used > > > I am brand new to R and have a very limited knowledge of functions and > syntax. Maybe the if statement is not even the right way of tackling this > problem.... > > Thanks in advance for your answers! > > Léa > > -- > View this message in context: > http://r.789695.n4.nabble.com/Trouble-creating-and-adjacency-matrix-tp3842106p3842106.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
See end of message. On 26/09/11 10:07, Spartina wrote:> Hello all, > > I'm having trouble creating an adjacency matrix. > > Basically, I need to turn the following distance matrix into an adjacency > matrix based on whether values are>1.5 or not. If they are>1.5, then the > returned value should be 0. If they are =<1.5, then the returned value > should be 1. > > DistanceMatrix: > > A B C D E > [1,] 0.000000 2.828427 1.581139 2.236068 2.000000 > [2,] 2.828427 0.000000 1.581139 4.123106 2.000000 > [3,] 1.581139 1.581139 0.000000 2.549510 2.121320 > [4,] 2.236068 4.123106 2.549510 0.000000 4.123106 > [5,] 2.000000 2.000000 2.121320 4.123106 0.000000 > > I'm trying to do this via the if statement, however I can't get it to work > and it keeps returning an error message: > >> if(DistanceMatrix>1.5) {0} else {1} > [1] 1 > Warning message: > In if (DistanceMatrix> 1.5) { : > the condition has length> 1 and only the first element will be used > > > I am brand new to R and have a very limited knowledge of functions and > syntax. Maybe the if statement is not even the right way of tackling this > problem....Well, you got that right. It isn't. Do: AdjacencyMatrix <- 0 + (DistanceMatrix <= 1.5) Note that the ``0 + '' bizzo is a trick to coerce a logical matrix (entries "TRUE" or "FALSE") into a numeric matrix of 0-s and 1-s. Learn to speak R. It's worth the effort. cheers, Rolf Turner
Hello all, Thanks for your answers! I'll give your suggestions a try later on today. Cheers, L?a -- View this message in context: http://r.789695.n4.nabble.com/Trouble-creating-and-adjacency-matrix-tp3842106p3843660.html Sent from the R help mailing list archive at Nabble.com.