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

Georg Kindermann Georg@K|nderm@nn @end|ng |rom gmx@@t
Tue May 9 19:26:45 CEST 2023


Thanks!
With data.table I'm not able to create it.

DT <- data.table::data.table(id = 1:2)
DT$ar <- array(1:8, c(2,2,2))
#Error in set(x, j = name, value = value) : 
#  Supplied 8 items to be assigned to 2 items of column 'ar'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

DFA <- data.frame(id = 1:2)
DFA[["ar"]] <- array(1:8, c(2,2,2))
data.table::as.data.table(DFA)
#Error in setDT(ans, key = key) : 
#  All elements in argument 'x' to 'setDT' must be of same length, but the profile of input lengths (length:frequency) is: [0:1, 2:2]
#The first entry with fewer than 2 entries is 3

But with tibble it works as expected.

TI <- tibble::tibble(id = 1:2, ar = array(1:8, c(2,2,2)))
str(TI[1,])
#tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
# $ id: int 1
# $ ar: int [1, 1:2, 1:2] 1 3 5 7

But it would be nice if something similar would also work with data.frame.

Georg
 
 
 

Gesendet: Dienstag, 09. Mai 2023 um 16:56 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

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 



More information about the R-help mailing list