-2

I have written the following code to declare an array as data frame:

b=as.data.frame(array(0,dim=c(NF,29,1,T+1),
dimnames=list(NULL,c(…..varnames))))

Now, I am not able to move inside the array.. for instance, if I need to show all the matrices in the following array position [,,1,1], what I need to write? I have tried code like:

b$[].1.1
b$,1.1
b[,,1,1]"

but, of course, it does not work. Thank you very much for your help!

curveball
  • 4,320
  • 15
  • 39
  • 49
Lorenzo
  • 3
  • 3

2 Answers2

0

from ?as.data.frame :

Arrays can be converted to data frames. One-dimensional arrays are treated like vectors and two-dimensional arrays like matrices. Arrays with more than two dimensions are converted to matrices by ‘flattening’ all dimensions after the first and creating suitable column labels.

array1 <- array(1:8,dim = c(2,2,2),dimnames = split(paste0(rep(letters[1:2],each=3),1:3),1:3))
# , , 3 = a3
# 
#     2
# 1    a2 b2
#   a1  1  3
#   b1  2  4
# 
# , , 3 = b3
# 
#     2
# 1    a2 b2
#   a1  5  7
#   b1  6  8
# 

df1    <- as.data.frame(array1)
#    a2.a3 b2.a3 a2.b3 b2.b3
# a1     1     3     5     7
# b1     2     4     6     8

df1$b2.a3
# [1] 3 4
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
0

I need to create the a data frame, starting from an array which dimension is (2,3,1,3):

0   0   0   0   0   0   0   0   0  
0   0   0   0   0   0   0   0   0

Hence, the output that I need is:

debt loan stock debt loan stock debt loan stock
  0    0    0    0    0     0     0    0    0
  0    0    0    0    0     0     0    0    0

Is next code correct?

b=array(0, dim=c(3,3,1,4), dimnames=list(NULL,c("debt","loan","stock")))
output=as.data.frame(b)
Lorenzo
  • 3
  • 3