[R] How to combine conditional argument and logical argument in R to create subset of data...

arun smartpink111 at yahoo.com
Wed Mar 6 05:36:15 CET 2013


Hi,

 b[b[,4]>15 & (b[,1]>4|is.na(b[,1])) & (b[,2]>4|is.na(b[,2])),]
 #    [,1] [,2] [,3] [,4] [,5]
#[1,]    6   NA   NA   16   20
#[2,]   NA    5   NA   17   21
A.K.


----- Original Message -----
From: HJ YAN <yhj204 at googlemail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, March 5, 2013 9:33 PM
Subject: [R] How to combine conditional argument and logical argument in R to create subset of data...

Dear R user

I have data created using code below

b<-matrix(2:21,nrow=4)
b[,1:3]=NA
b[4,2]=5
b[3,1]=6

Now the data is

> b
         [,1]  [,2]   [,3]  [,4]  [,5]
[1,]   NA   NA   NA   14   18
[2,]   NA   NA   NA   15   19
[3,]      6   NA   NA   16   20
[4,]   NA    5     NA    17   21


I want to keep data in column 4 greater than 15 and the value in column 1 &
2 either greater than 4 or is 'NA'. So I would like to have
my outcome as below...

[3,]   6   NA NA 16 20
[4,] NA 5 NA 17 21

I thought something like the code below gonna to work but it only returns
the last row,e.g "NA 5 NA 17 21". ...

bb<-b[which( (b[,2]>4 | b[,2]==NA) & (b[,1]>4 | b[,1]==NA) & b[,4]>15) ,])


Please could anyone help?

Many thanks in advance

HJ

    [[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.




More information about the R-help mailing list