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.kinderm...@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.4...@gmail.com> > An: "Georg Kindermann" <georg.kinderm...@gmx.at> > Cc: "Rui Barradas" <ruipbarra...@sapo.pt>, r-help@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]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.