[R] data frame with nested data frame

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 24 00:22:09 CET 2010


On Thu, Dec 23, 2010 at 5:06 PM, Vadim Ogranovich
<vogranovich at jumptrading.com> wrote:
> Dear R-users,
>
> I am somewhat puzzled by how R treats data frames with nested data frames. Below are a couple of examples, maybe someone could help explain what the guiding logic here is.
>
> ## construct plain data frame
>> z <- data.frame(x=1)
>
> ## add a data frame member
>> z$y <- data.frame(a=1,b=2)
>
> ## puzzle 1: z is apparently different from a straightforward construction of the 'same' object
>> all.equal(z, data.frame(x=1,y=data.frame(a=1,b=2)))
> [1] "Names: 1 string mismatch"                                                       "Length mismatch: comparison on first 2 components"
> [3] "Component 2: Modes: list, numeric"                                              "Component 2: names for target but not for current"
> [5] "Component 2: Attributes: < Modes: list, NULL >"                                 "Component 2: Attributes: < names for target but not for current >"
> [7] "Component 2: Attributes: < Length mismatch: comparison on first 0 components >" "Component 2: Length mismatch: comparison on first 1 components"
>
> ## puzzle 2: could not rbind z
>> rbind.data.frame(z, z)
> Error in `row.names<-.data.frame`(`*tmp*`, value = c("1", "1")) :
>  duplicate 'row.names' are not allowed
> In addition: Warning message:
> non-unique value when setting 'row.names': '1'
>

1. If we strip out all data frames and leave them as lists then
(a) z is basically a nested list list(x=1,y=list(a=1,b=2)) whereas
(b)  the construct
  data.frame(x=1,y=data.frame(a=1,b=2)))
is interpreted to be a flat list, namely, the same as:
  data.frame(x = 1, y.a = 1, y.b = 2)
and if we strip out data frames is basically list(x = 1, y.a = 1, y.b = 2)

2. Although this may be nothing more than stating the obvious, it
seems its not necessarily true that operations that work in the normal
cases also work in strange uncommon nested cases like this.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list