R 从列表创建数据帧

R 从列表创建数据帧,r,list,dataframe,R,List,Dataframe,我有3个列表,我想将它们组合成一个数据框架,其中每一列都是列表的一个元素 有3个列表(ld是数值为112的数字符号) ld无法创建带有列表列的data.frames是一个长期困扰。按首选项的降序排列,您可以 使用tible包中的data\u frame tibble::data_frame(or, thetax, thetay) 按照David Arenburg的建议,创建一个包含维度的列表,然后将其转换为数据帧 data.frame(cbind(or, thetax, thetay))

我有3个列表,我想将它们组合成一个数据框架,其中每一列都是列表的一个元素

有3个列表(ld是数值为112的数字符号)


ld无法创建带有列表列的
data.frame
s是一个长期困扰。按首选项的降序排列,您可以

  • 使用
    tible
    包中的
    data\u frame

    tibble::data_frame(or, thetax, thetay)
    
  • 按照David Arenburg的建议,创建一个包含维度的列表,然后将其转换为数据帧

    data.frame(cbind(or, thetax, thetay))
    
  • 创建数据框,然后添加列表列

    df3 <- data.frame(1:8)[,FALSE]
    df3$or <- or
    df3$or <- thetax
    df3$or <- thetay
    

    • 我可能遗漏了一些东西,但我认为这将作为向量而不是列表。这对你有用吗,还是我偏离了目标

      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 )
      

      x我知道你已经接受了答案,但是没有简单的方法从列表中创建data.frame,这不是事实。
      使用您的示例的简单方法是:

      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
      

      ld未显示
      ld
      。因此,也许
      data.frame(or=unlist(or),thetax=unlist(thetax),thetay=unlist(thetay))
      您的示例失败了
      有趣的错误(X[[i]],…):找不到对象“ld”
      -您需要提供
      ld
      。这些都需要列表吗?我看不出你为什么用列表而不是向量。向量会更快、更整洁。相关帖子:也可以做
      data.frame(sappy(list(or,thetax,thetay),unlist))
      如果你没有混合类型的话,它可以使用向量而不是列表(顺便说一句,列表也是向量)。但是他们有列表的问题是,问题是我有列表请看下面我的答案,找到一种从列表创建data.frame列的简单方法。没关系,@David Arenburg指出,这会在data.frames中生成列表…如果OP想将它们保留为列表,你可以只做
      data.frame(cbind(或,thetax,thetay))
      我想是吧。虽然我不明白为什么首先要把它转换成一个
      data.frame
      。请重新考虑运行
      str(df)
      ,也许这正是OP真正想要的,Dunnoth这并不是我想要的,但感谢您的洞察力。顺便说一句,如果OP只想将它们保留为列表列,您可以执行
      data.frame(cbind(或,thetax,thetay))
      structure(
        list(or, thetax, thetay), 
        class = "data.frame", 
        row.names = .set_row_names(8)
      )
      
      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 )
      
      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