[R] Multiple column/row names?

Henrik Bengtsson hb at stat.berkeley.edu
Tue Aug 19 05:30:08 CEST 2008


You can only have one set of column names and one set of row names per
matrix.  This is also true for data.frame, but for data.frame you can
at least use one of the columns to store a 2nd set of "row names".

Typically people store extra annotation like what you are asking for
externally and then at the end to the translation; assuming there is a
one-to-one relationship between the attributes your are describing.

However, if you really want to do it, you can do some tricks by
encoding the two vectors in one vector and decode them afterward, e.g.

> names1 <- sprintf("foo%02d", 1:6);
[1] "foo01" "foo02" "foo03" "foo04" "foo05" "foo06"
> names2 <- sprintf("bar%02d", 6:1);
[1] "bar06" "bar05" "bar04" "bar03" "bar02" "bar01"
> names <- paste(names1, names2, sep=";");
[1] "foo01;bar06" "foo02;bar05" "foo03;bar04" "foo04;bar03" "foo05;bar02"
[6] "foo06;bar01"

> x <- matrix(1:6, nrow=6, ncol=4);
> rownames(x) <- names;
> x
            [,1] [,2] [,3] [,4]
foo01;bar06    1    1    1    1
foo02;bar05    2    2    2    2
foo03;bar04    3    3    3    3
foo04;bar03    4    4    4    4
foo05;bar02    5    5    5    5
foo06;bar01    6    6    6    6
> y <- x[1:3,];
> y
            [,1] [,2] [,3] [,4]
foo01;bar06    1    1    1    1
foo02;bar05    2    2    2    2
foo03;bar04    3    3    3    3

> rownames(y)
[1] "foo01;bar06" "foo02;bar05" "foo03;bar04"
> t <- strsplit(rownames(y), split=";", fixed=TRUE);
> t <- matrix(unlist(t), ncol=length(t));
> t
     [,1]    [,2]    [,3]
[1,] "foo01" "foo02" "foo03"
[2,] "bar06" "bar05" "bar04"

/Henrik

On Mon, Aug 18, 2008 at 7:40 PM, Hesen Peng <hesen.peng at gmail.com> wrote:
> Ahhhh, I'm doing this because of the project I'm working on. The gene
> names in clinical genomic usually have two specifications for one
> entry. And I was just wondering whether there is a simple way of
> solving that problem.
>
> 2008/8/19 jim holtman <jholtman at gmail.com>:
>> The row names are easy since they are just one line.  The column names
>> I don't think can be constructed. Why do you want two column names?
>> You won't be able to access them.  You can always create your own
>> object and put the two column names on it.
>>
>> On Mon, Aug 18, 2008 at 1:20 PM, Hesen Peng <hesen.peng at gmail.com> wrote:
>>> Hi,
>>>
>>> I'm thinking of presenting the matrix like:
>>>
>>>                         Col1.1    Col1.2    Col1.3
>>>                         Col2.1    Col2.2    Col2.3
>>> Row1.1 Row1.2  123        321        221
>>> Row2.1 Row2.2  234        432        223
>>>
>>> Here the Row*.* and Col*.* may be the names of the corresponding
>>> rows/columns. Thanks
>>>
>>> 2008/8/13 jim holtman <jholtman at gmail.com>:
>>>> Exactly what are you asking?  Can you provide an example of the output
>>>> you would expect.
>>>>
>>>> On Tue, Aug 12, 2008 at 1:21 AM, Hesen Peng <hesen.peng at gmail.com> wrote:
>>>>> Hi all,
>>>>>
>>>>> I wonder if there is a way to create a matrix with two (or even more)
>>>>> column/row names? Thank you very much.
>>>>>
>>>>> Have a nice day.
>>>>>
>>>>> --
>>>>> 彭河森 Hesen Peng
>>>>> http://hesen.peng.googlepages.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.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Jim Holtman
>>>> Cincinnati, OH
>>>> +1 513 646 9390
>>>>
>>>> What is the problem that you are trying to solve?
>>>>
>>>
>>>
>>>
>>> --
>>> 彭河森 Hesen Peng
>>> http://hesen.peng.googlepages.com/
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>
>
>
> --
> 彭河森 Hesen Peng
> http://hesen.peng.googlepages.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