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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue May 9 09:32:56 CEST 2023


À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
> 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



More information about the R-help mailing list