[R] data.frame with a column containing an array
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Thu May 25 19:15:00 CEST 2023
I really don't know. I would call it a request for extended capabilities of
[.data.frame, rather than a feature or bug. But maybe wiser heads than mine
who monitor this list can sort it out.
-- Bert
On Wed, May 24, 2023 at 8:52 PM Georg Kindermann <Georg.Kindermann using gmx.at>
wrote:
> So is this an expected behavior or is it a bug which should be reported
> somewhere else?
>
> Thanks!
> Georg
>
>
>
> Gesendet: Dienstag, 09. Mai 2023 um 19:28 Uhr
> Von: "Bert Gunter" <bgunter.4567 using gmail.com>
> An: "Georg Kindermann" <Georg.Kindermann using gmx.at>
> Cc: "Rui Barradas" <ruipbarradas using sapo.pt>, r-help using r-project.org
> Betreff: Re: [R] data.frame with a column containing an array
>
>
>
> I think the following may provide a clearer explanation:
>
> subs <- c(1,3)
> DFA <- data.frame(id = 1:3)
> ar <- array(1:12, c(3,2,2))
> ## yielding
> > ar
> , , 1
>
> [,1] [,2]
> [1,] 1 4
> [2,] 2 5
> [3,] 3 6
>
> , , 2
>
> [,1] [,2]
> [1,] 7 10
> [2,] 8 11
> [3,] 9 12
>
> ## array subscripting gives
> > ar[subs,,]
> , , 1
>
> [,1] [,2]
> [1,] 1 4
> [2,] 3 6
>
> , , 2
>
> [,1] [,2]
> [1,] 7 10
> [2,] 9 12
>
> ## Now with df's
> > DFA[["ar"]] <- ar
> >
> > DFM <- data.frame(id = 1:3)
> > DFM[["M"]] <- matrix(1:6, nc =2)
> >
> > str(DFM)
> 'data.frame': 3 obs. of 2 variables:
> $ id: int 1 2 3
> $ M : int [1:3, 1:2] 1 2 3 4 5 6
> > str(DFA)
> 'data.frame': 3 obs. of 2 variables:
> $ id: int 1 2 3
> $ ar: int [1:3, 1:2, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
> >
> > ## But the data frame print method for these give
> > DFM
> id M.1 M.2
> 1 1 1 4
> 2 2 2 5
> 3 3 3 6
> > DFA
> id ar.1 ar.2 ar.3 ar.4
> 1 1 1 4 7 10
> 2 2 2 5 8 11
> 3 3 3 6 9 12
> >
> > ## [.data.frame subscripting gives
> > DFA[subs,]
> id ar
> 1 1 1
> 3 3 3
> > DFM[subs,]
> id M.1 M.2
> 1 1 1 4
> 3 3 3 6
> >
> > ## but explicit array subscripting of course works
> > DFA$ar[match(subs,DFA$id),,]
> , , 1
>
> [,1] [,2]
> [1,] 1 4
> [2,] 3 6
>
> , , 2
>
> [,1] [,2]
> [1,] 7 10
> [2,] 9 12
>
>
> Cheers,
> Bert
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list