[R] data.frame with a column containing an array

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu May 25 23:47:36 CEST 2023


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 using 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 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]]
>
>______________________________________________
>R-help using 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.



More information about the R-help mailing list