Reputation: 417
Hi I have a quick question about converting a list of data.frames into an array (I prefer the latter format for plotting and manipulation).
I have the following code which just about works but I am not getting the loop right and wondered if someone could point me to what I am doing wrong.
What I want to do is fill an array with the values of one of the columns from each of the different data.frames in my list of data.frames.
# list all the files in a directory
data.dir = "/data/output"
# list all the files to plot
files <- list.files(data.dir, full=TRUE, pattern="Fields_grid*")
# cat them together into one data frame
data <- lapply( files, read.csv, header=FALSE, skip=26)
# calc no. of files
nfiles <- length(files)
# set up an empty array to fill with data
z <- array( NA, dim=c(length(x), length(y), nfiles ))
# loop through all the data.frames/files
for (i in 1:length(nfiles)) {
# calc index's of lists
x <- sort(unique( data[[i]]$V3))
y <- sort(unique( data[[i]]$V4))
indx <- match( data[[i]]$V3, x)
indy <- match( data[[i]]$V4, y)
# fill array
z[ cbind( indx, indy, i)] <- data[[i]]$V5 }
I know this is a terrible question as I can not reproduce the result I get, but I hope you can understand my issue. When I look at z[,,1] this contains data but z[,,2] to z[,,nfiles] contains NA (i.e. my loop has not worked). I guess its something to do with the indexing but I really can't see what I have done wrong.
Again apologies for an opaque question and thanks for having a look!
Upvotes: 2
Views: 3718
Reputation: 89057
The problem is with your loop:
for (i in 1:length(nfiles)) { [...]
Here, length(nfiles)
is 1
so your loop is only run for i
equals 1
.
Instead, you meant to do
for (i in 1:nfiles) { [...]
or
for (i in seq_len(nfiles)) { [...]
or
for (i in seq_along(files)) { [...]
Upvotes: 3