I have multiple files containing the data in tabulated form. I want to generate a 3-dimensional array in which the data from each file stored in the third dimension. For example, if I have 10 files then data from the first file will store in the first layer of 3D array, data from the second file in the second layer and so on.
Here is the dummy code I am using but it does not work correctly.
# reading data from the file ( I have a list of files names as fname)
dataDum <- read.table(fname[i],header = F, sep =';', skip=121, stringsAsFactors = FALSE)
# Assigning data to the array. I have already generated an empty array with the desired dimension
finaldata[, , i]=dataDum
It is not clear "why" your code is not working properly, as there is not reproducible example. As it is it should work correctly given the inputs are as expected. For example:
arr <- array(data = 0, dim = c(10,10,3));
for(i in 1:3){
mat <- matrix(rnorm(10^2), ncol = 10);
arr[,,i] <- mat
}
arr
If an error occurs it is likely due to dataDum
being a data.frame
. Explicitly using as.matrix(dataDum)
would fix such an issue.