1

Given a list x:

$a
[1] 1 2 3 4 5 6
$b
[1] 10 20 30 40 50
$c
[1] 100 200 300 400 500

I want to construct a data frame that contains one column containing the following values:

1 10 100

Namely the elements of the column come from the first element in x$a, x$b and x$c.

I wonder what is the most efficient way to construct this column?

c2709c
  • 47
  • 4
  • This can help you a little. [What is the most efficient way to cast a list as a data frame?](http://stackoverflow.com/questions/4512465/what-is-the-most-efficient-way-to-cast-a-list-as-a-data-frame) – Ronak Shah Nov 01 '16 at 12:20

2 Answers2

0

We can use [ to extract the 1st element

d1 <- data.frame(Col1 = unname(sapply(x, `[`, 1)))
d1
#   Col1
#1    1
#2   10
#3  100

We can also do

data.frame(Col1 = do.call(cbind, x)[1,])
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can try this too:

data.frame(Col1=do.call(rbind, x)[,1])
  Col1
a    1
b   10
c  100
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63