How do I extract rownames from a matrix?

时间秒杀一切 提交于 2020-01-02 01:21:10

问题


I have a matrix with rownames that are dates. I want to extract these row names into a variable, and then use rownames() to apply these dates to another matrix I have. Let's say the matrix is called 'data.matrix'.

Whenever I run:

data.matrix[,0]

I get a printout of all the dates. So I do this:

v <- data.matrix[,0]

When I return v I get a nice list of all the dates. But when I use:

rownames(other.matrix) <- v

And then I return:

head(other.matrix)

I don't get any new column names.

Also, when I try:

head(v)

I get NULL

But when I do:

v

I get a nice printout of all my dates.

So what gives? At first I thought that matrices and dates were incompatible but it seems as if they are.

Right now I'm using merge() in this way to add dates:

z <- merge(v, other.matrix)

But it feels like there's a better way to do this.


回答1:


The command

data.matrix[,0]

does return a matrix object without columns. Hence, you see its row names only.

To extract the rownames from an object, use the rownames function:

v <- rownames(data.matrix)


来源:https://stackoverflow.com/questions/14301498/how-do-i-extract-rownames-from-a-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!