Hi, the R code: a <- matrix(c(1,5,4,3,7,10,34,4,3,8,6,5,12,17,45,3,2,45,46,47,3,4,22,12,21), nrow=5) rownames(a) <- c("a","b","c","d","e") a a[which(a[,3] < 8), ] a[which(a[,3] < 6), ] produces the following output:> a[,1] [,2] [,3] [,4] [,5] a 1 10 6 3 3 b 5 34 5 2 4 c 4 4 12 45 22 d 3 3 17 46 12 e 7 8 45 47 21> a[which(a[,3] < 8), ][,1] [,2] [,3] [,4] [,5] a 1 10 6 3 3 b 5 34 5 2 4> a[which(a[,3] < 6), ][1] 5 34 5 2 4 I actually thought it must be rather easy to somehow extract the row name when the result is just a vector as in the second example and not a matrix. I tried several ways but without any success. Any advice? Best, syrvn -- View this message in context: http://r.789695.n4.nabble.com/how-to-get-row-name-of-matrix-when-result-is-a-vector-tp2525631p2525631.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2010-Sep-03 13:21 UTC
[R] how to get row name of matrix when result is a vector
Try this: a[a[,3] < 6,, drop = FALSE] On Fri, Sep 3, 2010 at 10:08 AM, syrvn <mentor_@gmx.net> wrote:> > Hi, > > the R code: > > a <- > matrix(c(1,5,4,3,7,10,34,4,3,8,6,5,12,17,45,3,2,45,46,47,3,4,22,12,21), > nrow=5) > rownames(a) <- c("a","b","c","d","e") > a > a[which(a[,3] < 8), ] > a[which(a[,3] < 6), ] > > > produces the following output: > > > a > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > c 4 4 12 45 22 > d 3 3 17 46 12 > e 7 8 45 47 21 > > > a[which(a[,3] < 8), ] > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > > > a[which(a[,3] < 6), ] > [1] 5 34 5 2 4 > > > I actually thought it must be rather easy to somehow extract the row name > when the result is just a vector as in the second example and not a matrix. > I tried several ways but without any success. > > Any advice? > Best, > syrvn > > > -- > View this message in context: > http://r.789695.n4.nabble.com/how-to-get-row-name-of-matrix-when-result-is-a-vector-tp2525631p2525631.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi, I do not understand the code right now but it does its job. Thanks a lot! Best, syrvn -- View this message in context: http://r.789695.n4.nabble.com/how-to-get-row-name-of-matrix-when-result-is-a-vector-tp2525631p2525657.html Sent from the R help mailing list archive at Nabble.com.
Ivan Calandra
2010-Sep-03 13:28 UTC
[R] how to get row name of matrix when result is a vector
Hi! I don't really understand your goal, but if you want to extract the row names, rownames(a[which(a[,3] < 8), ]) does it. But I think you've already tried it. Maybe it's just me... HTH Ivan Le 9/3/2010 15:08, syrvn a écrit :> Hi, > > the R code: > > a<- matrix(c(1,5,4,3,7,10,34,4,3,8,6,5,12,17,45,3,2,45,46,47,3,4,22,12,21), > nrow=5) > rownames(a)<- c("a","b","c","d","e") > a > a[which(a[,3]< 8), ] > a[which(a[,3]< 6), ] > > > produces the following output: > >> a > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > c 4 4 12 45 22 > d 3 3 17 46 12 > e 7 8 45 47 21 > >> a[which(a[,3]< 8), ] > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > >> a[which(a[,3]< 6), ] > [1] 5 34 5 2 4 > > > I actually thought it must be rather easy to somehow extract the row name > when the result is just a vector as in the second example and not a matrix. > I tried several ways but without any success. > > Any advice? > Best, > syrvn > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]
R doesn't simply treat a row vector as a matrix. -- View this message in context: http://r.789695.n4.nabble.com/how-to-get-row-name-of-matrix-when-result-is-a-vector-tp2525631p2525666.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Sep-03 13:51 UTC
[R] how to get row name of matrix when result is a vector
On Sep 3, 2010, at 9:08 AM, syrvn wrote:> > Hi, > > the R code: > > a <- > matrix > (c(1,5,4,3,7,10,34,4,3,8,6,5,12,17,45,3,2,45,46,47,3,4,22,12,21), > nrow=5) > rownames(a) <- c("a","b","c","d","e") > a > a[which(a[,3] < 8), ] > a[which(a[,3] < 6), ]It wasn't exactly clear and possible that you wanted only rownames that satisfied those criteria > rownames(a)[which(a[,3] < 8)] [1] "a" "b" > rownames(a)[which(a[,3] < 6)] [1] "b" > rownames(a)[which(a[,3] < 4)] character(0) Sometimes which() is needed, and others it is not. This appears to be one where it is not needed: > rownames(a)[a[,3] < 4] character(0) > rownames(a)[a[,3] < 6] [1] "b" > rownames(a)[a[,3] < 8] [1] "a" "b" -- David.> > > produces the following output: > >> a > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > c 4 4 12 45 22 > d 3 3 17 46 12 > e 7 8 45 47 21 > >> a[which(a[,3] < 8), ] > [,1] [,2] [,3] [,4] [,5] > a 1 10 6 3 3 > b 5 34 5 2 4 > >> a[which(a[,3] < 6), ] > [1] 5 34 5 2 4 > > > I actually thought it must be rather easy to somehow extract the row > name > when the result is just a vector as in the second example and not a > matrix. > I tried several ways but without any success. > > Any advice? > Best, > syrvn > > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to-get-row-name-of-matrix-when-result-is-a-vector-tp2525631p2525631.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT