Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidimensional arrays of data frames

I like to define an 3D-array of data.frame(A,B,C,...) so that I can do

for (x in 1:4)
  for (y in 1:5)
    for (z in 1:5) {
       m[x,y,z]$A <- dnorm(1)
       m[x,y,z]$B <- dnorm(1)
       m[x,y,z]$C <- dnorm(1)
    }

it would be ok too, if I get a data.frame(x,y,z,A,B,C) with x,y,z ids and a short and effizient way to manipulate and read any line "x,y,z".

Perhaps there are better ideas? I like to get rid of

mA[x,y,z] <- ...
mB[x,y,z] <- ...
mC[x,y,z] <- ...
like image 925
ckluss Avatar asked Dec 02 '25 23:12

ckluss


1 Answers

A more standard format would be a data frame with 6 columns --- x, y, z, A, B, and C. You can achieve this with:

dat <- expand.grid(x=1:4, y=1:5, z=1:5, A=dnorm(1), B=dnorm(1), C=dnorm(1))
head(dat)
#   x y z         A         B         C
# 1 1 1 1 0.2419707 0.2419707 0.2419707
# 2 2 1 1 0.2419707 0.2419707 0.2419707
# 3 3 1 1 0.2419707 0.2419707 0.2419707
# 4 4 1 1 0.2419707 0.2419707 0.2419707
# 5 1 2 1 0.2419707 0.2419707 0.2419707
# 6 2 2 1 0.2419707 0.2419707 0.2419707
like image 187
josliber Avatar answered Dec 04 '25 15:12

josliber