Hi, I am having some problems using the if statement correctly. I have used it many times previously so I dona't know what is different with this case. Here is my problem: I have a 1X10 matrix of values as follows:> H.MC[,1] [1,] 4.257669 [2,] 7.023242 [3,] 4.949857 [4,] 5.107000 [5,] 4.257669 [6,] 4.257669 [7,] 4.257669 [8,] 4.257669 [9,] 4.257669 [10,] 4.257669 What I want to do is replace all the values if 4.257669 with a random number between 3 and 4.5. To do this I have: H.MC.fin <- matrix(0,10,1) for (j in 1:10) { if(H.MC[j] == 4.257669) H.MC.fin[j] <-runif(1,3,4.5) else H.MC.fin[j] <- H.MC[j] } This doesn't seem to do anything and H.MC.fin is the same as H.MC. Does anyone know what I am doing wrong? Thanks, Doug -- View this message in context: http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html Sent from the R help mailing list archive at Nabble.com.
Hi, matrix has a 2 dimensions. Is this work: a<-matrix(rep(c(1,2),c(5,5)),ncol=1) dim(a) for (i in 1:10) { ifelse(a[i,]==1, a[i,]<-runif(1,3,4.5), a[i,]) } a Andrija On Tue, Mar 8, 2011 at 1:07 PM, dpender <d.pender.1@research.gla.ac.uk>wrote:> Hi, > > I am having some problems using the if statement correctly. I have used it > many times previously so I dona't know what is different with this case. > Here is my problem: > > I have a 1X10 matrix of values as follows: > > > H.MC <http://h.mc/> > [,1] > [1,] 4.257669 > [2,] 7.023242 > [3,] 4.949857 > [4,] 5.107000 > [5,] 4.257669 > [6,] 4.257669 > [7,] 4.257669 > [8,] 4.257669 > [9,] 4.257669 > [10,] 4.257669 > > What I want to do is replace all the values if 4.257669 with a random > number > between 3 and 4.5. To do this I have: > > H.MC.fin <- matrix(0,10,1) > > for (j in 1:10) { > > if(H.MC <http://h.mc/>[j] == 4.257669) H.MC.fin[j] <-runif(1,3,4.5) else > H.MC.fin[j] <- > H.MC <http://h.mc/>[j] > > } > > This doesn't seem to do anything and H.MC.fin is the same as H.MC<http://h.mc/> > . > > Does anyone know what I am doing wrong? > > Thanks, > > Doug > > > -- > View this message in context: > http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Mar 8, 2011, at 7:07 AM, dpender wrote:> Hi, > > I am having some problems using the if statement correctly. I have > used it > many times previously so I dona't know what is different with this > case. > Here is my problem: > > I have a 1X10 matrix of values as follows: > >> H.MC > [,1] > [1,] 4.257669 > [2,] 7.023242 > [3,] 4.949857 > [4,] 5.107000 > [5,] 4.257669 > [6,] 4.257669 > [7,] 4.257669 > [8,] 4.257669 > [9,] 4.257669 > [10,] 4.257669 > > What I want to do is replace all the values if 4.257669 with a > random number > between 3 and 4.5. To do this I have: > > H.MC.fin <- matrix(0,10,1) > > for (j in 1:10) { > > if(H.MC[j] == 4.257669)Testing for equality of floating point numbers is a common source of errors. See the FAQ. -- David.> H.MC.fin[j] <-runif(1,3,4.5) else H.MC.fin[j] <- > H.MC[j] > > } > > This doesn't seem to do anything and H.MC.fin is the same as H.MC. > > Does anyone know what I am doing wrong? > > Thanks, > > Doug > > > -- > View this message in context: http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 West Hartford, CT
On Tue, Mar 08, 2011 at 04:07:03AM -0800, dpender wrote:> Hi, > > I am having some problems using the if statement correctly. I have used it > many times previously so I dona't know what is different with this case. > Here is my problem: > > I have a 1X10 matrix of values as follows: > > > H.MC > [,1] > [1,] 4.257669 > [2,] 7.023242 > [3,] 4.949857 > [4,] 5.107000 > [5,] 4.257669 > [6,] 4.257669 > [7,] 4.257669 > [8,] 4.257669 > [9,] 4.257669 > [10,] 4.257669 > > What I want to do is replace all the values if 4.257669 with a random number > between 3 and 4.5. To do this I have: > > H.MC.fin <- matrix(0,10,1) > > for (j in 1:10) { > > if(H.MC[j] == 4.257669) H.MC.fin[j] <-runif(1,3,4.5) else H.MC.fin[j] <- > H.MC[j] > > } > > This doesn't seem to do anything and H.MC.fin is the same as H.MC.Hi. It is likely that none of the numbers in H.MC is exactly 4.257669. This is easy to check by printing H.MC - 4.257669 The condition H.MC[j] == 4.257669 is satisfied for those numbers, which are printed as 0. If there are numbers close to 4.257669, but not exactly equal, try to determine the maximum difference from 4.257669, which should still be interpreted as equality. The function all.equal(), which is suggested for comparison of numeric values, reports the numbers as equal, if their relative difference is at most (approx) 1.5e-8. Use it, for example, as isTRUE(all.equal(H.MC[j], 4.257669)) If an absolute error is more suitable, try using abs(H.MC[j] - 4.257669) < 1e-10 with a possibly different bound. Hope this helps. Petr Savicky.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of dpender > Sent: Tuesday, March 08, 2011 4:07 AM > To: r-help at r-project.org > Subject: [R] If Statement > > Hi, > > I am having some problems using the if statement correctly. > I have used it > many times previously so I dona't know what is different with > this case. > Here is my problem: > > I have a 1X10 matrix of values as follows: > > > H.MC > [,1] > [1,] 4.257669 > [2,] 7.023242 > [3,] 4.949857 > [4,] 5.107000 > [5,] 4.257669 > [6,] 4.257669 > [7,] 4.257669 > [8,] 4.257669 > [9,] 4.257669 > [10,] 4.257669 > > What I want to do is replace all the values if 4.257669 with > a random number > between 3 and 4.5. To do this I have: > > H.MC.fin <- matrix(0,10,1) > > for (j in 1:10) { > > if(H.MC[j] == 4.257669) H.MC.fin[j] <-runif(1,3,4.5) else > H.MC.fin[j] <- > H.MC[j] > > }Your for loop can be replaced by: whichToReplace <- H.MC == 4.257669 H.MC.fin <- H.MC # copy the whole thing # then replace some values in the copy H.MC.fin[whichToReplace] <- runif(sum(whichToReplace), 3, 45) whichToReplace will be a logical vector the length of H.MC, with a TRUE value at the positions to be replaced. Summing a logical vector gives the number of TRUE's in it. Others have pointed out that the initial test would be better done by something like whichToReplace <- abs(H.MC - 4.257669) < 0.00001 instead of demanding exact equality. If there might be NA's in H.MC, following the test with whichToReplace <- !is.na(whichToReplace) & whichToReplace Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> This doesn't seem to do anything and H.MC.fin is the same as H.MC. > > Does anyone know what I am doing wrong? > > Thanks, > > Doug > > > -- > View this message in context: > http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >