The main problem you have is that as you look over i in 1:6766 you are re-initializing all the list elements to empty list()s again (ie mask1$vid[[v]] = list() will clear out whatever was in the [[v]] index from an earlier loop). So you need to separate initialization from accumulation. You also have a lot of redundant code.
Here is one possible alternative that uses a list for the 1 MASKs and that uses a 3-D list array for vid rather than a list of lists of lists.
M<-5
F<-199
V<-34
J<-8
#full initialization
mask <- lapply(1:M, function(x) {
list(
vid=array(list(NULL),dim=c(V,F,J)),
all=lapply(1:J, function(x) numeric(0)))
})
mask_dir<-list(m1=character(F*V), m2=character(F*V), m3=character(F*V), m4=character(F*V),m5=character(F*V))
read.table.fake<-function(...) {return(data.frame(matrix(rep(1:3, each=J), nrow=J, ncol=3)))}
for(f in 1:F) {
for(v in 1:V) {
i<- f + (v-1)*199
data<-lapply(1:M, function(x) t(read.table.fake(mask_dir[[x]][i], i)))
for(j in 1:J) {
ppdata <- lapply(1:M, function(x) Filter(function(z) {z>0}, data[[x]][,j]))
for(m in 1:M) {
mask[[m]]$vid[[v,f,j]] <- ppdata[[m]]
mask[[m]]$all[[j]] <- c(mask[[m]]$all[[j]], ppdata[[m]])
}
}
}
}
I've included a fake read.table because you really didn't specify what your data actually looked like. I assume each file must have 8 rows and perhaps a variable number of columns and all values are numeric? I also combined the mask_dir variables into a list such that mask_dir=list(mask1_dir, mask2_dir, mask3_dir, mask4_dir, mask5_dir)
So the code is shorter but it will take a while to run because that's a lot of looping and file reading.
To get at elements, you could use mask[[5]]$vid[[3,1,2]]
rather than mask5$vid[[3]][[1]][[2]]
and mask[[2]]$all[[3]]
rather than mask2$all[[3]]
.
To test, i'd start with small values of F and V just to make sure things look good first.