1

I have a list of elements called results1. Each element has a column and row names. This elements are vectors.

class(results1)
[1] "list"
> class(results1[[1]])
[1] "numeric"
is.vector(results1[[1]])
[1] TRUE

results1[[1]]
      ..a15.pdf       ..a17.pdf       ..a18.pdf       ..a21.pdf 
     1.27679608      1.05090176      1.51820192      2.30296037 

I want to convert each vector to a data frame of two columns, with the row names (denoted ..a15.pdf etc. above) being one of the columns. I can do it with a separate element/vector:

j<-as.data.frame(results1[[1]])
j<-cbind(j,names=rownames(j))
rownames(j) <- c()

But I can not achieve it for the entire list. I tried

results1<-lapply(results1, function(x)cbind(x, names=rownames(x)))

I can see two columns

results1[[1]]
                           x
..a15.pdf         1.27679608
..a17.pdf         1.05090176
..a18.pdf         1.51820192

But apparently this does not create a data frame.

is.data.frame(results1[[1]])
[1] FALSE

And as I understand it the ..15pdf is not a column

results1[[1]][,1]
      ..a15.pdf       ..a17.pdf       ..a18.pdf       ..a21.pdf 
     1.27679608      1.05090176      1.51820192      2.30296037 

I tried then to convert my vectors to data frames first:

results1<-lapply(results1, function(x)as.data.frame(x[1]))
results1<-lapply(results1, function(x){as.data.frame(x[1])})

But it did not work. As stated above, I'd like to convert rownames from each vector to a column and delete them. The output should be a list of data frames, each df having two columns. The rownames column should be named 'names', if you need a name. Thanks. Here is dput for the first two vectors of my list

dput(results1[c(1,2)])
list(c(..a15.pdf = 1.27679607834331, ..a17.pdf = 1.05090175857491, 
..a18.pdf = 1.51820192474905, ..a21.pdf = 2.30296037386815, ..a2TTT.pdf = 1.48568731934637, 
..a5.pdf = 0.493713103224402, ..B11.pdf = 1.02705905465749, ..B12.pdf = 0.999747360884078, 
..B13.pdf = 2.40828101927852, ..B22.pdf = 0.695152132033603, 
..B24.pdf = 2.1436001615064, ..B4.pdf = 2.25444037842867, ..B7.pdf = 0.909773940025014, 
..B8.pdf = 1.14837173756827, `..cw10-1.pdf` = -1.36323271003293, 
`..cw15-1TTT.pdf` = 0.341428535787024, `..cw17-1.pdf` = -0.786878348480425, 
..cw18.pdf = 0.793720472787986, ..cw3.pdf = -1.57831038567642, 
..cw4.pdf = 0.277733503122777, ..cw7_1TTT.pdf = -0.0364645818969112, 
`..cw13-1.pdf` = -18.336668416705), c(..a15.pdf = 2.096687569578, 
..a17.pdf = 2.19826038300833, ..a18.pdf = 1.91814204277357, ..a21.pdf = 0.801448541154512, 
..a2TTT.pdf = 2.16169560949165, ..a5.pdf = 1.48585130705963, 
..B11.pdf = 0.95126061691997, ..B12.pdf = 1.93116618236938, ..B13.pdf = 1.92555316191766, 
..B22.pdf = 1.00560861920225, ..B24.pdf = 2.91129684208931, ..B4.pdf = 2.75687804718002, 
..B7.pdf = 1.31164431967781, ..B8.pdf = 2.22449059765255, `..cw10-1.pdf` = -1.22629519335285, 
`..cw15-1TTT.pdf` = 1.31168579553008, `..cw17-1.pdf` = -17.5786422399896, 
..cw18.pdf = 1.25323523754693, ..cw3.pdf = -0.754445550651364, 
..cw4.pdf = 0.555577381430987, ..cw7_1TTT.pdf = 0.577850999404076, 
`..cw13-1.pdf` = -34.2662973287062))
Vasile
  • 1,017
  • 2
  • 10
  • 19

3 Answers3

2

We can use map with enframe

library(purrr)
library(tibble)
map(results1, enframe)

-output

[[1]]
# A tibble: 22 x 2
   name        value
   <chr>       <dbl>
 1 ..a15.pdf   1.28 
 2 ..a17.pdf   1.05 
 3 ..a18.pdf   1.52 
 4 ..a21.pdf   2.30 
 5 ..a2TTT.pdf 1.49 
 6 ..a5.pdf    0.494
 7 ..B11.pdf   1.03 
 8 ..B12.pdf   1.00 
 9 ..B13.pdf   2.41 
10 ..B22.pdf   0.695
# … with 12 more rows

[[2]]
# A tibble: 22 x 2
   name        value
   <chr>       <dbl>
 1 ..a15.pdf   2.10 
 2 ..a17.pdf   2.20 
 3 ..a18.pdf   1.92 
 4 ..a21.pdf   0.801
 5 ..a2TTT.pdf 2.16 
 6 ..a5.pdf    1.49 
 7 ..B11.pdf   0.951
 8 ..B12.pdf   1.93 
 9 ..B13.pdf   1.93 
10 ..B22.pdf   1.01 
# … with 12 more rows
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Create a dataframe in each list and remove the rownames -

lapply(results1, function(x) {
  tmp <- data.frame(names=names(x), x)
  rownames(tmp) <- NULL
  tmp
})

#[[1]]
#             names            x
#1        ..a15.pdf   1.27679608
#2        ..a17.pdf   1.05090176
#3        ..a18.pdf   1.51820192
#4        ..a21.pdf   2.30296037
#5      ..a2TTT.pdf   1.48568732
#6         ..a5.pdf   0.49371310
#7        ..B11.pdf   1.02705905
#8        ..B12.pdf   0.99974736
#...
#...

#[[2]]
#             names           x
#1        ..a15.pdf   2.0966876
#2        ..a17.pdf   2.1982604
#3        ..a18.pdf   1.9181420
#4        ..a21.pdf   0.8014485
#5      ..a2TTT.pdf   2.1616956
#...
#...

A shorter alternative would be -

lapply(results1, stack)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks. it works. You probably remember you tried to do it blindly yesterday. the bit that was different yesterday in your attemmpt: mat <- cbind.data.frame(names = rownames(x), x). I am curious what made the difference. using data.frame instead of cbind.data.frame or not using rownames function? – Vasile Jun 04 '21 at 08:40
  • Yes, I do remember that. Yesterday, I was assuming that you had list of matrix but you actually had a list of numeric vector. – Ronak Shah Jun 04 '21 at 08:50
0

This question could be similar to this. Answer of @hrbrmstr is concise to convert the row names to a column using rownames_to_column() function.

library(tibble)
df <- rownames_to_column(.data, var = "rowname")
Mehmet Yildirim
  • 471
  • 1
  • 4
  • 17