“Embedded” data.frame in R. What is it, what is it called, why does it behave the way it does?

柔情痞子 提交于 2019-12-03 11:19:16

I think the strucutre makes it pretty clear

str(df)
# 'data.frame':   5 obs. of  4 variables:
#  $ ID  : int  1 2 3 4 5
#  $ var1: chr  "a" "b" "c" "d" ...
#  $ var2:'data.frame':   5 obs. of  2 variables:
#   ..$ var2a: chr  "v" "w" "x" "y" ...
#   ..$ var2b: chr  "vv" "ww" "xx" "yy" ...
#  $ var3: chr  "aa" "bb" "cc" "dd" ...

It's a data.frame with a column (var2) that contains a data.frame. This isn't super easy to create so i'm not quite sure how you did it but it isn't technically "illegal" in R.

data.frames can contain matrices and other data.frames. So it doesn't just look at the length() of the elements, it looks at the dim() of the elements to see if it has the right number of "rows".

I often "fix" or expand these data.frames using

fixed <- do.call("data.frame", df)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!