Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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 ?

like image 846
user2806363 Avatar asked Oct 12 '25 06:10

user2806363


1 Answers

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).

like image 110
MrFlick Avatar answered Oct 14 '25 21:10

MrFlick