I have a list that contains several matrices. The rows in each matrix are unique, but the columns represent variables that are common across each matrix.
The example below provides an example of the type of data I have:
mat1 <- matrix(sample(1:100, 10, replace=TRUE), 2, 5)
mat2 <- matrix(sample(1:100, 15, replace=TRUE), 3, 5)
mat3 <- matrix(sample(1:100, 20, replace=TRUE), 4, 5)
lst <- list(mat1, mat2, mat3)
Changing the colnames on any single matrix within the list is easy:
colnames(lst[[1]]) <- LETTERS[1:5]
Thus, it seems like I should be able to write a simple lapply:
lst <- lapply(lst, function(x) colnames(x) <- LETTERS[1:5]
But this over-writes the original values rather than re-naming the columns. A for loop will do the trick:
for(i in 1:length(lst)){colnames(lst[[i]]) <- LETTERS[1:5]}
But since my real data has lists containing several hundred matrices, this approach seems inefficient. Any way to accomplish this w/ lapply? It seems like I must be missing something simple. Any help you can provide will be greatly appreciated.