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

后端 未结 1 852
盖世英雄少女心
盖世英雄少女心 2021-02-13 22:37

I have the following data structure in R:

df <- structure(
  list(
    ID = c(1L, 2L, 3L, 4L, 5L),
    var1 = c(\'a\', \'b\', \'c\', \'d\', \'e\'),
    var2 =          


        
1条回答
  •  有刺的猬
    2021-02-13 23:09

    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)
    

    0 讨论(0)
提交回复
热议问题