[R] Multiple conditional without if

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Feb 27 21:59:24 CET 2007


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Petr 
> Klasterecky
> Sent: Tuesday, February 27, 2007 12:15 PM
> To: bunny , lautloscrew.com
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Multiple conditional without if
> 
> bunny , lautloscrew.com napsal(a):
> > Dear all,
> > 
> > i am stuck with a syntax problem.
> > 
> > i have a matrix which has about 500 rows and 6 columns.
> > now i want to kick some data out.
> > i want create a new matrix which is basically the old one 
> except for all
> > entries which have a 4 in the 5 column AND a 1 in the 6th column.
> > 
> > i tried the following but couldn´t get a new matrix, just 
> some wierd  
> > errors:
> > 
> > 
> newmatrix=oldmatrix[,2][oldmatrix[,5]==4]&&oldmatrix[,2][oldma
> trix[,6] 
> > ==1]
> This is nonsense.
> 
>  > m <- matrix(rep(1:12,3),ncol=6)
>  > m[1,6] <-1
>  > m
>       [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    1    7    1    7    1    1
> [2,]    2    8    2    8    2    8
> [3,]    3    9    3    9    3    9
> [4,]    4   10    4   10    4   10
> [5,]    5   11    5   11    5   11
> [6,]    6   12    6   12    6   12
> 
>  > m[!((m[,5]==4)|(m[,6]==1)),]

I think what was requested was to exclude rows where column 5 was 4 AND column 6 was 1, so,

m[((m[,5]!=4)|(m[,6]!=1)),]

<<<snip>>>

Hope this is helpful,

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA  98504-5204



More information about the R-help mailing list