I think that when you read "D1.txt", "D2.txt"................"D45.txt" in your files "D1.txt", "D2.txt"................"D45.txt" , they are converted to matrices, and therefore your particular for loop is not working. I will use your example:
L <- replicate(5, expand.grid(1:10, 1:10)[sample(100, 10), ], simplify=FALSE)
If we use class(L[[1]]) to select the first element of the list, it will output [1] "data.frame" , if you use a for loop in this list that contains only data.frames , you will not see an error. and it will give you what you want. If, however, we convert all the elements into a list into matrices:
for(i in seq_along(L)){ L[[i]] <- as.matrix(L[[i]]) }
and using class(L[[1]]) it will output [1] "matrix" . If you use the for for loop now on L , which now contains matrices, we get:
> L <- lapply(seq_along(L), function(i) { + L[[i]][, paste0('DF', i)] <- 1 + L[[i]] + }) Error in `[<-`(`*tmp*`, , paste0("DF", i), value = 1) : subscript out of bounds
Therefore, you can either make sure that when you read in your files, they are forced to data.frames , use the @Richards solution or read in your files and force them to data.frames via
for(i in seq_along(L)){ L[[i]] <- as.data.frame(L[[i]]) }
and use the for loop.