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 at 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 at gmail.com> > An: "Georg Kindermann" <Georg.Kindermann at gmx.at> > Cc: "Rui Barradas" <ruipbarradas at sapo.pt>, r-help at 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]]
Maybe... if you want it to act like a matrix... then you should... use a matrix... I think making this behaviour work with 2d arrays could be a feature, but making it work with higher dimension arrays would be difficult, and differentiating between 2d and higher dimension arrays could have just as unexpected behaviors as differentiating between matrices and 2d arrays is for you now. Arrays and matrices are supposed to be more compatible now than they used to be... specifying a matrix should work just as well as specifying a 2d array when it comes to whatever operations are leading you to want an array at all, and will close the door on trying to stuff a higher dimension array into the dataframe accidentally. ... or don't put it into a data frame? On May 25, 2023 10:15:00 AM PDT, Bert Gunter <bgunter.4567 at gmail.com> wrote:>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 at 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 at gmail.com> >> An: "Georg Kindermann" <Georg.Kindermann at gmx.at> >> Cc: "Rui Barradas" <ruipbarradas at sapo.pt>, r-help at 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 at 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.-- Sent from my phone. Please excuse my brevity.
Dear Bert! Thanks for the answer. Just let me summarize the current (4.3.0) behavior. DF <- data.frame(id = 1:2) DF[["ar"]] <- array(1:8, c(2,2,2)) #Converts array to vector #Removes dim and takes selected elements from first column DF[1,] # id ar #1 1 1 str(DF[1,]) #'data.frame': 1 obs. of 2 variables: # $ id: int 1 # $ ar: int 1 #Converts array to vector #Removes dim and takes selected elements DF[c(TRUE, FALSE),] # id ar #1 1 1 #Warning message: #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : # corrupt data frame: columns will be truncated or padded with NAs str(DF[c(TRUE, FALSE),]) #'data.frame': 1 obs. of 2 variables: # $ id: int 1 # $ ar: int 1 3 5 7 #Converts array to vector #Removes dim and takes selected elements from first column and #adds all elements form the remaining data DF[-2,] # id ar #1 1 1 #Warning message: #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : # corrupt data frame: columns will be truncated or padded with NAs str(DF[-2,]) #'data.frame': 1 obs. of 2 variables: # $ id: int 1 # $ ar: int 1 3 4 5 6 7 8 #Converts array to vector #Removes dim but keeps all elements DF[TRUE,] # id ar #1 1 1 #2 2 2 #Warning message: #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : # corrupt data frame: columns will be truncated or padded with NAs str(DF[TRUE,]) #'data.frame': 2 obs. of 2 variables: # $ id: int 1 2 # $ ar: int 1 2 3 4 5 6 7 8 Kind regards, Georg Gesendet:?Donnerstag, 25. Mai 2023 um 19:15 Uhr Von:?"Bert Gunter" <bgunter.4567 at gmail.com> An:?"Georg Kindermann" <Georg.Kindermann at gmx.at> Cc:?"Rui Barradas" <ruipbarradas at sapo.pt>, r-help at r-project.org Betreff:?Re: Re: [R] data.frame with a column containing an array 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?
Please refer to Jeff Newmiller's response. I believe that your further exploration of indexing a data frame containing an array column demonstrates his point: don't do it! As this discussion could now devolve into a morass of personal opinion (such as the above), if you or others care to continue, please do so **offlist**. However, I have nothing further to say in any case. Cheers, Bert On Fri, May 26, 2023 at 7:23?AM Georg Kindermann <Georg.Kindermann at gmx.at> wrote:> > Dear Bert! > > Thanks for the answer. > Just let me summarize the current (4.3.0) behavior. > > > DF <- data.frame(id = 1:2) > DF[["ar"]] <- array(1:8, c(2,2,2)) > > #Converts array to vector > #Removes dim and takes selected elements from first column > DF[1,] > # id ar > #1 1 1 > > str(DF[1,]) > #'data.frame': 1 obs. of 2 variables: > # $ id: int 1 > # $ ar: int 1 > > > #Converts array to vector > #Removes dim and takes selected elements > DF[c(TRUE, FALSE),] > # id ar > #1 1 1 > #Warning message: > #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : > # corrupt data frame: columns will be truncated or padded with NAs > > str(DF[c(TRUE, FALSE),]) > #'data.frame': 1 obs. of 2 variables: > # $ id: int 1 > # $ ar: int 1 3 5 7 > > > #Converts array to vector > #Removes dim and takes selected elements from first column and > #adds all elements form the remaining data > DF[-2,] > # id ar > #1 1 1 > #Warning message: > #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : > # corrupt data frame: columns will be truncated or padded with NAs > > str(DF[-2,]) > #'data.frame': 1 obs. of 2 variables: > # $ id: int 1 > # $ ar: int 1 3 4 5 6 7 8 > > > #Converts array to vector > #Removes dim but keeps all elements > DF[TRUE,] > # id ar > #1 1 1 > #2 2 2 > #Warning message: > #In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x, : > # corrupt data frame: columns will be truncated or padded with NAs > > str(DF[TRUE,]) > #'data.frame': 2 obs. of 2 variables: > # $ id: int 1 2 > # $ ar: int 1 2 3 4 5 6 7 8 > > > Kind regards, > Georg > > > Gesendet: Donnerstag, 25. Mai 2023 um 19:15 Uhr > Von: "Bert Gunter" <bgunter.4567 at gmail.com> > An: "Georg Kindermann" <Georg.Kindermann at gmx.at> > Cc: "Rui Barradas" <ruipbarradas at sapo.pt>, r-help at r-project.org > Betreff: Re: Re: [R] data.frame with a column containing an array > > 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