3

I have 3 lists and I would like to combine them into a dataframe, where each column is an element of a list.

There are the 3 lists (ld is a nummeric with value 112)

ld <- 112

# 3 lists
or <- as.list(rep(0, 8))
thetax <- lapply(0:7, function(x) ld * cos (x * pi / 4))
thetay <- lapply(0:7, function(x) ld * sin (x * pi / 4))

Here is my attempt to combine them into a data.frame

 df3 <- data.frame(or, thetax, thetay)

I think this should be very simple and basic but I can't do it.

I have been searching for the answer for hours this and have been trying different things but nothing seems to work so far, and I can't execute it.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Devang Akotia
  • 119
  • 10
  • The `ld` is not shown. So, perhaps `data.frame(or = unlist(or), thetax = unlist(thetax), thetay = unlist(thetay))` – akrun Oct 05 '16 at 07:13
  • Your example fails `Error in FUN(X[[i]], ...) : object 'ld' not found` - you need to provide `ld`. – jakub Oct 05 '16 at 07:14
  • Do these need to be lists at all? I can't see the reason you're working with lists instead of vectors. Vectors would be faster and tidier. – rosscova Oct 05 '16 at 07:23
  • Relavant post: http://stackoverflow.com/questions/4227223/r-list-to-data-frame – zx8754 Oct 05 '16 at 07:43
  • 1
    Could also do `data.frame(sapply(list(or, thetax, thetay), unlist))` if you don't have mixed types there – David Arenburg Oct 05 '16 at 07:56
  • sorry for the misunderstanding about 'ld'. I wrote on the questions that " (ld is a nummeric with value 112)", but I understand that is very visible. – Devang Akotia Oct 05 '16 at 08:12
  • What is your desired output? You want the columns in the data.frame to remain lists? Why would you want to store them in a data.frame then? Why not in a list? – David Arenburg Oct 05 '16 at 08:35

3 Answers3

4

The fact that you can't create data.frames with list columns is a longstanding annoyance. In decreasing order of preference, you can either

  • Use data_frame from the tibble package instead.

    tibble::data_frame(or, thetax, thetay)
    
  • Create a list with dimensions, then convert that to be a data frame, as suggested by David Arenburg.

    data.frame(cbind(or, thetax, thetay))
    
  • Create the data frame, then add the list columns afterwards.

    df3 <- data.frame(1:8)[,FALSE]
    df3$or <- or
    df3$or <- thetax
    df3$or <- thetay
    
  • Use structure to make the data frame.

    structure(
      list(or, thetax, thetay), 
      class = "data.frame", 
      row.names = .set_row_names(8)
    )
    
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
2

I may be missing something, but I would think this will work as vectors instead of lists. Does this work for you, or am I way off target?

x <- 0:7
ld <- 112
or <- rep(0, 8)
thetax <- ld * cos (x * pi / 4)
thetay <- ld * sin (x * pi / 4)
df3 <- data.frame( or, thetax, thetay )
rosscova
  • 5,430
  • 1
  • 22
  • 35
1

I realize you already accepted an answer, however it is not true that there is no easy way to create a data.frame from lists. A straightforward way using your example is:

ld <- 112
or <- as.list(rep(0, 8))
thetax <- lapply(0:7, function(x) ld * cos (x * pi / 4))
thetay <- lapply(0:7, function(x) ld * sin (x * pi / 4))

df <- as.data.frame(do.call("cbind",list(or, thetax, thetay)))
df

  V1            V2           V3
1  0           112            0
2  0      79.19596     79.19596
3  0  6.858022e-15          112
4  0     -79.19596     79.19596
5  0          -112 1.371604e-14
6  0     -79.19596    -79.19596
7  0 -2.057407e-14         -112
8  0      79.19596    -79.19596
tobiasegli_te
  • 1,413
  • 1
  • 12
  • 18