Converting Lists into Data Frames

I have been trying to find out a standard method to go back and forth between lists and data frames. For example, suppose I have this data frame ("b"):

> b
        x1       x2       x3       x4       x5       x6       x7       x8      x9      x10
1 10.99873 14.43390 31.63950 25.10335 5.566093 6.799074 6.139689 6.726143 15.7906 26.71740
2 10.78481 14.50069 31.62804 25.01933 5.568667 6.796837 6.145826 6.726623 15.7906 26.72056
3 10.78481 14.34121 32.04394 25.01649 5.569037 6.797972 6.146143 6.727226 15.7906 26.70711

> str(b)
'data.frame':	3 obs. of  10 variables:
 $ x1 : num  11 10.8 10.8
 $ x2 : num  14.4 14.5 14.3
 $ x3 : num  31.6 31.6 32
 $ x4 : num  25.1 25 25
 $ x5 : num  5.57 5.57 5.57
 $ x6 : num  6.8 6.8 6.8
 $ x7 : num  6.14 6.15 6.15
 $ x8 : num  6.73 6.73 6.73
 $ x9 : num  15.8 15.8 15.8
 $ x10: num  26.7 26.7 26.7

I tried to convert this into a list:

my_list <- as.list(as.data.frame(t(b)))

my_list

$V1
 [1] 10.998732 14.433901 31.639495 25.103354  5.566093  6.799074  6.139689  6.726143 15.790605 26.717399

$V2
 [1] 10.784809 14.500692 31.628044 25.019326  5.568667  6.796837  6.145826  6.726623 15.790605 26.720555

$V3
 [1] 10.784813 14.341210 32.043936 25.016488  5.569037  6.797972  6.146143  6.727226 15.790605 26.707106

Problem: Now, I want to convert this list back into a data frame :

reverse_b =  do.call(rbind.data.frame, my_list)

reverse_b

  c.10.9987317621708..10.7848089486361..10.7848130464554. c.14.4339011311531..14.5006921589375..14.3412095755339. c.31.6394952088594..31.6280444860458..32.043936163187.
1                                                10.99873                                                14.43390                                               31.63950
2                                                10.78481                                                14.50069                                               31.62804
3                                                10.78481                                                14.34121                                               32.04394
  c.25.1033542603254..25.019326120615..25.0164884477854. c.5.56609274446964..5.56866699457169..5.569037348032. c.6.79907412827015..6.79683712124825..6.79797211289406.
1                                               25.10335                                              5.566093                                                6.799074
2                                               25.01933                                              5.568667                                                6.796837
3                                               25.01649                                              5.569037                                                6.797972
  c.6.13968880474567..6.14582557976246..6.14614284038544. c.6.72614294290543..6.72662328183651..6.72722597420216. c.15.7906045913696..15.7906046062708..15.7906046062708.
1                                                6.139689                                                6.726143                                                 15.7906
2                                                6.145826                                                6.726623                                                 15.7906
3                                                6.146143                                                6.727226                                                 15.7906
  c.26.7173993140459..26.7205552458763..26.7071058899164.
1                                                26.71740
2                                                26.72056
3                                                26.70711

And this look nothing like the original data frame "b"

Can someone please show me what I am doing wrong?

Thanks!

I think was able to figure it out!

> reverse_b = data.frame(do.call(rbind, my_list))

> reverse_b

X1 X2 X3 X4 X5 X6 X7 X8 X9 X10

V1 10.99873 14.43390 31.63950 25.10335 5.566093 6.799074 6.139689 6.726143 15.7906 26.71740

V2 10.78481 14.50069 31.62804 25.01933 5.568667 6.796837 6.145826 6.726623 15.7906 26.72056

V3 10.78481 14.34121 32.04394 25.01649 5.569037 6.797972 6.146143 6.727226 15.7906 26.70711

Happy New Year Everyone!

Excellent!
Actually your original example was just the same, only that the column names were not what they were before :wink:
Happy new year as well!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.