if statement problem

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

if statement problem

reena
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement problem

Rui Barradas
reena wrote
Hello,

I want to do fisher test for the rows in data file which has value less than 5 otherwise chi square test .The p values from both test should be stored in one resulted file. but there is some problem with bold if statement. I don't know how
implement this line properly.


x = cbind(obs1,obs2,exp1,exp2)
a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)        #matrix with initialized values

for (i in 1: length(x[,1]))
{
      if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) < 5)
           
Hello,
Try

if(any(x[i,] <5))

Merry Christmas
Rui Barradas

Reply | Threaded
Open this post in threaded view
|

Re: if statement problem

Patrick Burns
In reply to this post by reena
This is almost Circle 8.1.7 of
'The R Inferno':

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

but is making the mistake in the
other direction.

On 23/12/2011 22:40, reena wrote:

> Hello,
>
> I want to do fisher test for the rows in data file which has value less than
> 5 otherwise chi square test .The p values from both test should be stored in
> one resulted file. but there is some problem with bold if statement. I don't
> know how
> implement this line properly.
>
>
> x = cbind(obs1,obs2,exp1,exp2)
> a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)        #matrix with initialized
> values
>
> for (i in 1: length(x[,1]))
> {
>        *if((x[i,1] || x[i,2] || x[i,3] || x[i,4])<  5)*
>               {
>                   a[1,1]<- x[i,1];
>                   a[1,2]<- x[i,2];
>                   a[2,1]<- x[i,3];
>                   a[2,2]<- x[i,4];
>                   result<- fisher.test(a)
>                   write.table(result[["p.value"]],file="results.txt",
> sep="\n", append=TRUE, col.names=FALSE, row.names=FALSE);
>                 }
>
>               else
>              {
>                  a[1,1]<- x[i,1];
>                  a[1,2]<- x[i,2];
>                  a[2,1]<- x[i,3];
>                  a[2,2]<- x[i,4];
>                  result<- chisq.test(a)
>                  write.table(result[["p.value"]],file="results.txt",
> sep="\n", append=TRUE, col.names=FALSE, row.names=FALSE);}
>              }
>
> Regards
> R
>
> --
> View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

--
Patrick Burns
[hidden email]
twitter: @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: if statement problem

reena
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: if statement problem

Uwe Ligges-3


On 24.12.2011 12:03, reena wrote:
> It didn't work. :(

What did not work???

Please do not misuse the R-help mailing list! Its posting guide clearly
asks you to cite the thread and specify reproducible examples that make
other able to help.

Best,
Uwe Ligges



>
> --
> View this message in context: http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: if statement problem

Rui Barradas
In reply to this post by reena
Hello again.

I don't understand what didn't work.

First, it seems better to use 'nrow', the result is the same

 stopifnot(length(x[,1]) == nrow(x))

Then your multiple  OR condition.

#if((x[i,1] || x[i,2] || x[i,3] || x[i,4]) < 5)

x <- matrix(1:24, ncol=4)

for(i in 1:nrow(x))
        if(any(x[i,] < 5))
                cat("At least one is TRUE:", i, "\n")

for(i in 1:nrow(x))
        if((x[i,1] < 5) | (x[i,2] < 5) | (x[i,3] < 5) | (x[i,4] < 5))
                cat("The same with '|':", i, "\n")

In the second example each condition IS a condition. Unlike your original "compound" one.
The 'Introduction to R' clearly states '|' as the union of the logical expressions, The section is 2.4.
Also read Patrick's PDF, the parts I've read are great and very usefull.

(Thanks, Patrick, I was unaware of R inferno.pdf)

As a side note, you are duplicating the matrix 'a' assignement. Why not just before the 'if'?
Then, you could simply test if any 'a' is less than 5.

for (i in 1: row(x))
{
           a[1,1]  <- x[i,1];
           a[1,2] <- x[i,2];
           a[2,1] <- x[i,3];
           a[2,2] <- x[i,4];
           if(any(a < 5))
             {
                    etc...
   
(Or use the compound '|' ).

Rui Barradas