[R] Finding a vector position using names rather than values

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Fri Sep 12 10:32:59 CEST 2008


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



More information about the R-help mailing list