Jonsson, Pall
2008-Sep-12 08:22 UTC
[R] Finding a vector position using names rather than values
Dear R gurus, I have been struggling with this for a while and thought you might be able to give me some guidance. I have a data frame, on which I apply a row function. The result looks to me like a vector that retains the old row names. I then sort the vector and subsequently need to be able to identify the rank of certain values by searching for their row names. See this mock example:> dframe <- data.frame("Col1"=c(10,20,30), "Col2"=c(2,4,6), "Col3"=c(5,10,7), row.names=c("R1","R2","R3")) > row_median<-apply(dframe,1,median) > row_median.sorted <- sort(row_median, decreasing=TRUE)> row_median.sortedR2 R3 R1 10 7 5 I now want to know the rank of, say R3 in this vector. I know I can use match() to find this using the vector values, however I need to use the header names (eg. "R3") rather than the value (7). Many thanks for your help, Pall
Dimitris Rizopoulos
2008-Sep-12 08:32 UTC
[R] Finding a vector position using names rather than values
try this: dframe <- data.frame( Col1 = c(10,20,30), Col2 = c(2,4,6), Col3 = c(5,10,7), row.names = c("R1","R2","R3") ) mat <- data.matrix(dframe) rowMedians <- apply(mat, 1, median) rowMedians[names(rowMedians) %in% "R3"] I hope it helps. Best, Dimitris> Dear R gurus, > > I have been struggling with this for a while and thought you might be able > to give me some guidance. > > I have a data frame, on which I apply a row function. The result looks to > me like a vector that retains the old row names. I then sort the vector > and subsequently need to be able to identify the rank of certain values by > searching for their row names. See this mock example: > >> dframe <- data.frame("Col1"=c(10,20,30), "Col2"=c(2,4,6), >> "Col3"=c(5,10,7), row.names=c("R1","R2","R3")) >> row_median<-apply(dframe,1,median) >> row_median.sorted <- sort(row_median, decreasing=TRUE) > >> row_median.sorted > R2 R3 R1 > 10 7 5 > > I now want to know the rank of, say R3 in this vector. I know I can use > match() to find this using the vector values, however I need to use the > header names (eg. "R3") rather than the value (7). > > Many thanks for your help, > Pall > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Henrique Dallazuanna
2008-Sep-12 11:25 UTC
[R] Finding a vector position using names rather than values
Try this: order(row_median)[names(row_median) == "R3"] On Fri, Sep 12, 2008 at 5:22 AM, Jonsson, Pall <Pall.Jonsson@astrazeneca.com> wrote:> Dear R gurus, > > I have been struggling with this for a while and thought you might be able > to give me some guidance. > > I have a data frame, on which I apply a row function. The result looks to > me like a vector that retains the old row names. I then sort the vector and > subsequently need to be able to identify the rank of certain values by > searching for their row names. See this mock example: > > > dframe <- data.frame("Col1"=c(10,20,30), "Col2"=c(2,4,6), > "Col3"=c(5,10,7), row.names=c("R1","R2","R3")) > > row_median<-apply(dframe,1,median) > > row_median.sorted <- sort(row_median, decreasing=TRUE) > > > row_median.sorted > R2 R3 R1 > 10 7 5 > > I now want to know the rank of, say R3 in this vector. I know I can use > match() to find this using the vector values, however I need to use the > header names (eg. "R3") rather than the value (7). > > Many thanks for your help, > Pall > > ______________________________________________ > 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]]