How to read multiple files in a multidimensional array

I want to make an array in 3 dimensions.

Here is what I tried:

z<-c(160,720,420)
first_data_set <-array(dim = length(file_1), dimnames = z)

The data I read is on the same level. (only x and y) There are other data in the same format, and I need to place them in the same array with the first data. Therefore, as soon as I finish reading all the data, all of them are in the same array, but there is no rewriting.

So, I think the array should be 3 dimensions; otherwise, I cannot store all the data that I read in the loop.

+3
source share
1 answer

Say you have two 3x4 matrices:

m1 <- matrix(rnorm(12), nrow = 3, ncol = 4)
m2 <- matrix(rnorm(12), nrow = 3, ncol = 4)

If you want to put them in an array, first create an array from NA:

A <- array(as.numeric(NA), dim = c(3,4,2))

Then fill the layers with data:

A[,,1] <- m1
A[,,2] <- m2

As suggested by @Justin, you can also just put matrices into a list:

A2 <- list()
A2[['m1']] <- m1
A2[['m2']] <- m2

: , . , , csv:

myfiles <- dir(pattern = ".csv")
for (i in 1:length(myfiles)){
   A2[[myfiles[i]]] <- read.table(myfiles[i], sep = ',')
}
+7

All Articles