Devang Akotia Devang Akotia - 4 months ago 29
R Question

Create a data fram from lists

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.


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.

      list(or, thetax, thetay), 
      class = "data.frame", 
      row.names = .set_row_names(8)