3

I have a list of elements, like this

[[1]]
[1] 9.623571 5.334566 7.266597 6.510794 4.301958

[[2]]
[1] 9.693326 9.015892 1.266178 8.547392 4.326199

and I would like to transform it to dataframe like this:

V1       V2       V3       V4       V5
9.623571 5.334566 7.266597 6.510794 4.301958
9.693326 9.015892 1.266178 8.547392 4.326199

Therefore, all elements in the same position within the entries of the list, merged in the same column.

It has to be something related to "rbind", because the result is like doing:

rbind(list[[1]],list[[2]]...)

but I don't know how to apply it for all the entries of the list. Any light on this would be appreciated.

Thank you very much in advance.

Tina.

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
user18441
  • 643
  • 1
  • 7
  • 15

5 Answers5

7

since no one mentioned this yet:

 library(data.table)
 rbindlist(yourList)

Very fast.

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
6

Try this:

as.data.frame(do.call(rbind, l))

where, l <- list(1:5, 6:10)

#   V1 V2 V3 V4 V5
# 1  1  2  3  4  5
# 2  6  7  8  9 10
Arun
  • 116,683
  • 26
  • 284
  • 387
2

use sapply to get what you are after. As an example...

x <- list( 1:10 , letters[1:10])
as.data.frame( t( sapply(x , rbind) ) )

#     V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#   1  1  2  3  4  5  6  7  8  9  10
#   2  a  b  c  d  e  f  g  h  i   j
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
2

I know this is using an extra package, but I usually have plyr and reshape2 in my path.

library(plyr)
x <- list( sample(1:10,10) , sample(1:10,10))
ldply(x) # list 2 data.frame

  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  3  8  6  2 10  9  7  5  1   4
2  8 10  3  4  2  5  1  7  6   9

I find it particularly useful when the list has names:

names(x) <- c("hello","goodbye")
ldply(x) 

      .id V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   hello  3  8  6  2 10  9  7  5  1   4
2 goodbye  8 10  3  4  2  5  1  7  6   9

Using reshape2 you can also quickly reshape the data.frame into something that will plot quickly with ggplot2

library(reshape2)
melt(ldply(x),c(".id"))

       .id variable value
1    hello       V1     3
2  goodbye       V1     8
3    hello       V2     8
4  goodbye       V2    10
5    hello       V3     6
6  goodbye       V3     3
7    hello       V4     2
8  goodbye       V4     4
9    hello       V5    10
10 goodbye       V5     2
11   hello       V6     9
12 goodbye       V6     5
13   hello       V7     7
14 goodbye       V7     1
15   hello       V8     5
16 goodbye       V8     7
17   hello       V9     1
18 goodbye       V9     6
19   hello      V10     4
20 goodbye      V10     9

None of these solutions are particularly fast for very large datasets (untested assumption), but they are really useful when working with smallish datasets (< 10k rows).

Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
0

Use Reduce to rbind the elements of the list together:

l <- list (a=c(9.623571, 5.334566, 7.266597, 6.510794, 4.301958),
           b=c(9.693326, 9.015892, 1.266178, 8.547392, 4.326199))
# rbind the elements of the list.
Reduce(rbind, l)
Ista
  • 10,139
  • 2
  • 37
  • 38