user2806363
user2806363

Reputation: 2593

How to set row names for list of matrix or data frame?

I have list, which consists of data frame in every element of it. I want to set one of the column of evey data frame as rows name for it. but when I do this, I lose the dimension of my data.

Here is example of my data and my effort:

> ismr2[[1]][1:4,]
  mature           ID        RPM
1 mature MIMAT0000062 49791.5560
2 mature MIMAT0000063 92858.1285
3 mature MIMAT0000064 10418.8532
4 mature MIMAT0000065   404.7618
> 

My effort ;

ismr3<-lapply(ismr2, function(x){ row.names(x)<-as.character(x$ID) })


> dim(ismr3[[1]])
NULL
> 

dimension of first element of list before row.name operation is :

> dim(ismr2[[1]])
[1] 447   3

would someone help me to solve this probelm ?

Upvotes: 3

Views: 8396

Answers (1)

MrFlick
MrFlick

Reputation: 206576

I'm adding @Roland's answer here so the question appears answered.

ismr3 <- lapply(ismr2, function(x){ row.names(x)<-as.character(x$ID); x})

The important part is that the function inside lapply actually returns the value to be added to the list. With just the row.names<- assignment, the value returned from that is just the row names themselves. So because each assignment was returning a character vector, you were not creating an object that had a dim() attribute. (Atomic vectors have length, not dim).

Upvotes: 4

Related Questions