Hi Arun

Thank you so much for the help, that's really helpful!!

Also I have a quick question about the code below where I can not see why
it doesn't work...

I know the I shou

V1<-c(rep(111,4),rep(222,4),rep(333,4),rep(111,4),rep(222,4),rep(333,3))
V2<-c(1:23)
Tem1<-cbind(V1,V2)


So Tem 1 looks like...
> Tem1
       V1 V2
 [1,] 111  1
 [2,] 111  2
 [3,] 111  3
 [4,] 111  4
 [5,] 222  5
 [6,] 222  6
 [7,] 222  7
 [8,] 222  8
 [9,] 333  9
[10,] 333 10
[11,] 333 11
[12,] 333 12
[13,] 111 13
[14,] 111 14
[15,] 111 15
[16,] 111 16
[17,] 222 17
[18,] 222 18
[19,] 222 19
[20,] 222 20
[21,] 333 21
[22,] 333 22
[23,] 333 23

I would like the outcome to be...

      V1 V2

     111  1
     111  2
     111  3
     111  4
     111 13
     111 14
     111 15
     111 16
     222  5
     222  6
     222  7
     222  8
     222 17
     222 18
     222 19
     222 20
     333  9
     333 10
     333 11
     333 12
     333 21
     333 22
     333 23


So I tried code as below
------------------------------------------
Tem3<-c(NA,NA)
for(i in length(unique(Tem1[,1]))){
Tem2<-subset(Tem1,Tem1[,1]==unique(Tem1[,1])[i])
Tem3<-rbind(Tem3,Tem2)
Tem3
}
Tem4<-Tem3[-1,]
---------------------------------------

And only get this...


 V1 V2
 333  9
 333 10
 333 11
 333 12
 333 21
 333 22
 333 23


I tried to run the code step by step, e.g. letting i=1, then i=2, then i=
3, and updating my Tem3, I did get what I wanted, but wondered why in the
loop above it did not work...??


Many thanks in advance!

HJ














On Wed, Mar 6, 2013 at 4:36 AM, arun <smartpink...@yahoo.com> wrote:

> 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 <yhj...@googlemail.com>
> To: r-help@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@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]]

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

Reply via email to