Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create a sub-folder into which we create files.
- dir.create("temp_dir")
- setwd("temp_dir")
- # Create some files in temp_dir.
- sapply(1:10, FUN = function(i) {
- xy <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
- write.table(xy, file = sprintf("filename_%s.txt", i), row.names = FALSE, col.names = TRUE)
- })
- # Find relevant files, you can use pattern to match if folder doesn't contain only
- # files in question.
- all.files <- list.files(pattern = "filename_")
- # Read in all files. do not simplify the result because we'll need it "raw". The
- # result is a list.
- all.dfs <- sapply(all.files, FUN = read.table, header = TRUE, simplify = FALSE)
- # Calculate grand mean of the data.frame.
- all.means <- lapply(all.dfs, FUN = function(x) mean(sapply(x, mean)))
- # Corce from a list to a vector.
- one.mean <- as.numeric(all.means)
- # Calculate mean for each column.
- mean(one.mean)
- # Clean up this example.
- setwd("../")
- unlink("temp_dir")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement