[R] Multiple conditional without if [Broadcast]
Liaw, Andy
andy_liaw at merck.com
Tue Feb 27 21:14:38 CET 2007
From: bunny, lautloscrew.com
>
> 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]
>
> all i get is:
> numeric(0)
That's not a `weird error', but a numeric vector of length 0.
> does anybody have an idea how to fix this one ?
Try:
newmatrix = oldmatrix[oldmatrix[, 5]==4 & oldmatrix[, 6] == 1, 2, drop=FALSE]
If you just want a subset of column 2 as a vector, you can leave off the drop=FALSE part.
Reading "An Introduction to R" should have save you some trouble in the first place.
Andy
> thx in advance
>
> matthias
> [[alternative HTML version deleted]]
>
>
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}
More information about the R-help
mailing list