Solution - exercise 6 from 15.11.2019

# Skelleton script for processing several CSV files in a directory
# Frédéric Schütz

input_dir  <- "data"    # Directory from which we will get files
output_dir <- "output"  # Directory where we will save results

# Extract the list of all CSV files from the input_dir directory
files <- list.files(input_dir, "\\.csv$")

# Empty list in which we are going to store all our data
alldata <- list()

# Loop over all files
for (file in files) {
  cat("Processing file", file, "\n")

  # list.files (above) returns only the name of the file, not the
  # directory, so we have to add it.
  full_file_name <- paste0( input_dir, "/", file)

  # Open the file
  data <- read.table( full_file_name )

  # Do something with the file, and save the result in the specified
  # output directory

  # What we do is not important; here: we plot a histogram of the first
  # column of the data.frame being considered, and add vertical lines
  # indicating the mean and the median
  pdf( paste0( output_dir, "/", file, ".pdf"))
  abline(v=mean(data[,1]), col="red")
  abline(v=median(data[,1]), col="blue")

# Store the data in a list
  alldata <- c(alldata, list(data[,1]))

Last modified: Monday, 2 December 2019, 3:50 PM