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

Georg Kindermann
Thu, 25 May 2023 03:52:44 -0000

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?