为什么我的data.frame列是列表?

为什么我的data.frame列是列表?,r,dataframe,R,Dataframe,我有一个data.frame 'data.frame': 4 obs. of 2 variables: $ name:List of 4 ..$ : chr "a" ..$ : chr "b" ..$ : chr "c" ..$ : chr "d" $ tvd :List of 4 ..$ : num 0.149 ..$ : num 0.188 ..$ : num 0.161 ..$ : num 0.187 structure(list(name =

我有一个
data.frame

'data.frame':   4 obs. of  2 variables:
 $ name:List of 4
  ..$ : chr "a"
  ..$ : chr "b"
  ..$ : chr "c"
  ..$ : chr "d"
 $ tvd :List of 4
  ..$ : num 0.149
  ..$ : num 0.188
  ..$ : num 0.161
  ..$ : num 0.187

structure(list(name = list("a", "b", "c", 
    "d"), tvd = list(0.148831029536996, 0.187699857380692, 
    0.161428147003292, 0.18652668961466)), .Names = c("name", 
"tvd"), row.names = c(NA, -4L), class = "data.frame")
似乎
as.data.frame(lappy(z,unlist))
将其转换为通常的

'data.frame':   4 obs. of  2 variables:
 $ name: Factor w/ 4 levels "a",..: 4 1 2 3
 $ tvd : num  0.149 0.188 0.161 0.187
然而,我想知道我是否能做得更好。 我创建的丑陋数据框如下所示:

as.data.frame(do.call(rbind,lapply(my.list, function (m)
  list(name = ...,
       tvd = ...))))
我想知道是否可以修改此表达式,以便生成正常的数据表。

我建议这样做

do.call(rbind,lapply(my.list, function (m)
  data.frame(name = ...,
       tvd = ...)))

与其尝试将列表列表转换为data.frame,不如尝试删除原始数据,然后重新组装?如果是这样,这里有一些很酷的东西可以看。假设
df
是您的数据

data.frame
只是一个伪装的列表。要查看这一点,请将数据中的
df[[1]]
df$name
进行比较
[[
用于列表索引,以及
$
。因此,当我们在数据帧上使用
df$name
时,实际上是在查看列表项

> is.data.frame(df)  # df is a data frame
# [1] TRUE
> is.list(df)        # and it's also a list
# [1] TRUE

> x <- as.list(df)   # as.list() can be more useful than unlist() sometimes
                     # take a look at x here, it's a bit long

> (y <- do.call(cbind, x))  # reassemble to matrix form
#     name        tvd      
# [1,] "a"   0.148831 
# [2,] "b"  0.1876999
# [3,] "c"  0.1614281
# [4,] "d"  0.1865267

> as.data.frame(y)          # back to df
#   name       tvd
# 1    a  0.148831
# 2    b 0.1876999
# 3    c 0.1614281
# 4    d 0.1865267
>是.data.frame(df)#df是一个数据帧
#[1]是的
>is.list(df)#它也是一个列表
#[1]是的
>x