[R] Copying row names

Paul Smith phhs80 at gmail.com
Fri Sep 14 23:33:22 CEST 2007


On 9/14/07, kwaj <kingsley.oteng at gmail.com> wrote:
> I have been trying to copy the row names of one matrix to another matrix but
> having difficulty. The original matrix contains a row name which I would
> like to replicate in the new matrix. I use the following approach?
> The two matrices have identical dimensions.
>
> rN <- row.names(origMatrix)
> row.names(newMatrix) <- rN
>
> However the new matrix does not take on the labels.
>
> I have also tried,
>
> row.names(newMatrix) <- as.character(rN)
>
> Any ideas?

Use 'rownames' instead of 'row.names':

> a <- matrix(1:9,,3)
> rownames(a) <- c("x","y","z")
> b <- matrix(1:9,,3)
> names.of.a <- rownames(a)
> rownames(b) <- names.of.a
> a
  [,1] [,2] [,3]
x    1    4    7
y    2    5    8
z    3    6    9
> b
  [,1] [,2] [,3]
x    1    4    7
y    2    5    8
z    3    6    9
>

Paul



More information about the R-help mailing list