[R] Multiple conditional without if

Petr Klasterecky klaster at karlin.mff.cuni.cz
Tue Feb 27 21:15:05 CET 2007


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][oldmatrix[,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)),]
      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2    8    2    8    2    8
[2,]    3    9    3    9    3    9
[3,]    5   11    5   11    5   11
[4,]    6   12    6   12    6   12

Please read the appropriate chapter in R-intro to become familiar with 
vector, matrix and list indexing.
Petr


> 
> all i get is:
> numeric(0)
> 
> does anybody have an idea how to fix this one ?
> 
> thx in advance
> 
> matthias
> 	[[alternative HTML version deleted]]
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.


-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list