Grendel
Grendel

Reputation: 783

Change rownames ine matrix according to values in a list of characters

I have a matrix such as

> combined_ordered_matrix
  G1 G2 G3 G4 G5
NA1  NA  NA  NA  NA  NA
NA2  NA  NA  NA  NA  NA
NA3  NA  NA  NA  NA  NA
Sp4 0 0 0 0 0
Sp5 0 0 0 0 0
Sp6 0 0 0 0 0
NA4  NA  NA  NA  NA  NA

And a list of labels:

> labels(as.dendrogram(a))
  [1] "Sp1"     "Sp2"         
  [3] "Sp3"     "Sp4"         
  [5] "Sp4"     "Sp5"     
  [6] 'Sp6"     "Sp7"

class(as.dendrogram(a))
character

And I'm looking for a code in order to:

Replace rownames by the names present in the labels(as.dendrogram(a)) (in the same order)

Here I should get:

> combined_ordered_matrix
  G1 G2 G3 G4 G5
SP1  NA  NA  NA  NA  NA
Sp2  NA  NA  NA  NA  NA
Sp3  NA  NA  NA  NA  NA
Sp4 0 0 0 0 0
Sp5 0 0 0 0 0
Sp6 0 0 0 0 0
Sp7  NA  NA  NA  NA  NA

Upvotes: 0

Views: 25

Answers (1)

broti
broti

Reputation: 1382

I think this will solve your problem:

rownames(combined_ordered_matrix) <- labels(as.dendrogram(a))

Upvotes: 1

Related Questions