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) does anybody have an idea how to fix this one ? thx in advance matthias [[alternative HTML version deleted]]
Dear matthias, newmatrix = oldmatrix[ (oldmatrix[,5]==4 & oldmatrix[,6]==1) , ] Felipe -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of bunny , lautloscrew.com Sent: Tuesday, February 27, 2007 1:25 PM To: r-help at stat.math.ethz.ch Subject: [R] Multiple conditional without if 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) does anybody have an idea how to fix this one ? thx in advance matthias [[alternative HTML version deleted]]
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}}
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
Matthias, According to the logic, 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 newmatrix <- oldmatrix[ (oldmatrix[,5]!=4 & oldmatrix[,6]!=1) , ] --- newmatrix <- oldmatrix[ !(oldmatrix[,5]==4 & oldmatrix[,6]==1) , ] by, Felipe -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of bunny , lautloscrew.com Sent: Tuesday, February 27, 2007 1:25 PM To: r-help at stat.math.ethz.ch Subject: [R] Multiple conditional without if 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) does anybody have an idea how to fix this one ? thx in advance matthias [[alternative HTML version deleted]]
> -----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