Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlist and retain list names intact in R

Tags:

r

I have data similar to these:

listdata <- list(matrix(c(1,1,1,1,3,3,3,3),nrow=2,ncol=4),matrix(c(1,1,1,1,2,2,2,2),nrow=2))
names(listdata) <- c("5555","5566")

RowData <- lapply(listdata, "rownames<-", c("from","to"))

FrameData <- lapply(RowData, function(x) as.data.frame(x))

Now I do unlist the list as wanted, but the list names are changed. How to preserve the list names?

as.matrix(unlist(FrameData))

Wanted output without the .V11 column names:

       [,1]
 5555    1
 5555    1
 5555    1
 5555    1
 5555    3
 5555    3
 5555    3
  .....
  .....

EDIT: This is not good example since in my case I don't have the 5555.V1 but only adds only number: 1,2,3,4... So what I have actually is this:

    [,1]
 55551    1
 55552    1
 55553    1
 55554    1
 55555    3
 .... 
 555536   3
 555537   3
  .....

 Rname <- rownames(listdata)
 strtrim(Rname, nchar(Rname)-1)

the problem is that the numbering is increasing...so the problem is to define y nchar(x)-y

like image 842
Maximilian Avatar asked Dec 19 '25 14:12

Maximilian


1 Answers

library(reshape2)

melt(FrameData)
#Using  as id variables
#Using  as id variables
#   variable value   L1
#1        V1     1 5555
#2        V1     1 5555
#3        V2     1 5555
#4        V2     1 5555
#5        V3     3 5555
#6        V3     3 5555
#7        V4     3 5555
#8        V4     3 5555
#9        V1     1 5566
#10       V1     1 5566
#11       V2     1 5566
#12       V2     1 5566
#13       V3     2 5566
#14       V3     2 5566
#15       V4     2 5566
#16       V4     2 5566
like image 167
eddi Avatar answered Dec 21 '25 04:12

eddi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!