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

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue May 9 19:28:21 CEST 2023


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

On Tue, May 9, 2023 at 7:56 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> Right ... that's what I thought you meant.
>
> I'm pretty sure -- but not certain -- that columns of matrices are treated
> specially by [.data.frame, so that you have to explicitly index a higher
> dimensional array, e.g. like this:
>
>
> subs <- c(1,3)
> DFA <- data.frame(id = 1:3)
> DFA[["ar"]] <- array(1:12, c(3,2,2))
>
> DFA$ar[match(subs,DFA$id),,]
> ##yielding:
> , , 1
>
>      [,1] [,2]
> [1,]    1    4
> [2,]    3    6
>
> , , 2
>
>      [,1] [,2]
> [1,]    7   10
> [2,]    9   12
>
> You might check, e.g. the "data.table" package, to see if it indexes as
> you would like with columns that contain arrays.
>
> Alternatively, and perhaps preferably depending on your use case, you may
> wish to create a wholly different data structure or just treat the data
> frame as a list from the start. Data frames/matrix-like data structures are
> convenient and appropriate a lot of the time, but not always. R, like any
> flexible programming language, allows you -- even encourages you -- to
> create other data structures that fit your needs.
>
> Cheers,
> Bert
>
> On Tue, May 9, 2023 at 3:39 AM Georg Kindermann <Georg.Kindermann using gmx.at>
> wrote:
>
>> Thanks!
>>
>> No, to be consistent with what I get with a matrix I think it should be
>> like:
>>
>> x <- data.frame(id = DFA$id[1])
>> x$ar <- DFA$ar[1, , , drop = FALSE]
>>
>> str(x)
>> #'data.frame': 1 obs. of 2 variables:
>> # $ id: int 1
>> # $ ar: int [1, 1:2, 1:2] 1 3 5 7
>>
>> Georg
>>
>>
>>
>> Gesendet: Dienstag, 09. Mai 2023 um 09:32 Uhr
>> Von: "Rui Barradas" <ruipbarradas using sapo.pt>
>> An: "Georg Kindermann" <Georg.Kindermann using gmx.at>, r-help using r-project.org
>> Betreff: Re: [R] data.frame with a column containing an array
>> Às 11:52 de 08/05/2023, Georg Kindermann escreveu:
>> > Dear list members,
>> >
>> > when I create a data.frame containing an array I had expected, that I
>> get a similar result, when subsetting it, like having a matrix in a
>> data.frame. But instead I get only the first element and not all values of
>> the remaining dimensions. Differences are already when creating the
>> data.frame, where I can use `I` in case of a matrix but for an array I am
>> only able to insert it in a second step.
>> >
>> > DFA <- data.frame(id = 1:2)
>> > DFA[["ar"]] <- array(1:8, c(2,2,2))
>> >
>> > DFA[1,]
>> > # id ar
>> > #1 1 1
>> >
>> > DFM <- data.frame(id = 1:2, M = I(matrix(1:4, 2)))
>> >
>> > DFM[1,]
>> > # id M.1 M.2
>> > #1 1 1 3
>> >
>> > The same when trying to use merge, where only the first value is kept.
>> >
>> > merge(DFA, data.frame(id = 1))
>> > # id ar
>> > #1 1 1
>> >
>> > merge(DFM, data.frame(id = 1))
>> > # id M.1 M.2
>> > #1 1 1 3
>> >
>> > Is there a way to use an array in a data.frame like I can use a matrix
>> in a data.frame?
>> >
>> > I am using R version 4.3.0.
>> >
>> > Kind regards,
>> > Georg
>> >
>> > ______________________________________________
>> > 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[http://www.R-project.org/posting-guide.html]
>> > and provide commented, minimal, self-contained, reproducible code.
>> Hello,
>>
>> Are you looking for something like this?
>>
>>
>> DFA <- data.frame(id = 1:2)
>> DFA[["ar"]] <- array(1:8, c(2,2,2))
>>
>> DFA$ar[1, , ]
>> #> [,1] [,2]
>> #> [1,] 1 5
>> #> [2,] 3 7
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> ______________________________________________
>> 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.
>>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list