Re: [R] If Statement

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 08 Mar 2011 09:37:09 -0500

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 ______________________________________________ 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.
Received on Tue 08 Mar 2011 - 15:13:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Mar 2011 - 15:20:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive