[R] Retrieving matrix column and row names by index value

R. Michael Weylandt michael.weylandt at gmail.com
Thu Mar 29 20:40:07 CEST 2012


This seems to work:

namesFromIndex <- function(indx, obj){
    mapply(`[`, dimnames(obj), arrayInd(indx, dim(obj)))
}

Michael

On Thu, Mar 29, 2012 at 2:20 PM, Lanna Jin <lannajin at gmail.com> wrote:
> Hi all,
>
> So let's say I have a matrix, mdat and I only know the index number. How do
> I retrieve the column and row names?
>
> For example,
>> mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE,
>               dimnames = list(c("row1", "row2"),
>                               c("C.1", "C.2", "C.3")))
>> mdat[4]
> [1] 12
>> names(mdat[4])
> NULL
>> colnames(mdat[4])
> NULL
>> rownames(mdat[4])
> NULL
>> dimnames(mdat[4])
> NULL
>
> Thanks
>
>
> -----
> Lanna
> --
> View this message in context: http://r.789695.n4.nabble.com/Retrieving-matrix-column-and-row-names-by-index-value-tp4516390p4516390.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.



More information about the R-help mailing list