TL;DR

Write an R script which defines a function called “CSV_Col_Means”, which accepts a single argument - a file name which refers to a CSV file containing numeric variables. The function should return a vector of the column-wise means.

Introduction

One of the first major computational challenges which users of R face is the fact that, by default, R tends to store everything in memory, or RAM. This becomes a problem when the numeric representation of data in memory requires more space than is available. This challenge will address this issue.

Generating a Large Data Set

For a problem like this, even just generating a dummy data set can be challenging. Consider the following function:

GenerateDataSet1 <- function(N,P, file){
  means <- rt(P, df = 1, ncp = 10)^2
  X <- matrix(rpois(N*P, lambda = means), nrow = N, byrow = TRUE)
  write.csv(X, file = file)
}

This function will work, but it’s pretty slow:

system.time(GenerateDataSet1(100000, 10, "test.csv"))
##    user  system elapsed 
##    0.86    0.23    2.34

This is pretty slow, but more importantly it won’t let us generate data sets which are larger than our available RAM. For most workstations, there’s a lot more hard drive space (>100GB) than RAM (4-16GB). Here’s a version which breaks it up into batches:

GenerateDataSet2 <- function(N,P, infile, batch_size = 1000, verbose = FALSE){
  colnames <- paste(paste0("X", 1:P), collapse = ",")
  file_handle <- file(infile, open = "w")
  writeLines(colnames, file_handle)
  means <- rt(P, df = 1, ncp = 10)^2
  
  tot_recs <- 0
  while (tot_recs < N){
    to_write <- min(batch_size, N-tot_recs)
    Xdat <- apply(matrix(rpois(to_write*P, lambda = means), nrow = to_write, byrow = TRUE),
                1, paste0, collapse = ",")
    writeLines(Xdat, con = file_handle)
    tot_recs <- tot_recs + to_write
    if (verbose){
      cat(paste0(tot_recs, "\n"))
    }
  }
  close(file_handle)
}
system.time(GenerateDataSet2(100000, 10, batch_size = 200, "test.csv"))
##    user  system elapsed 
##    1.12    0.08    2.37

It’s a little bit faster, and we can make a considerably larger file:

system.time(GenerateDataSet2(10000000, 10, batch_size = 1000, "test.csv"))
##    user  system elapsed 
##   96.65    9.57  190.52

Task

Your job is to create an R function called “CSV_Col_Means”. This function should accept a single argument - a filename, which can be expected to point to a properly formatted csv file. The function should then compute the column wise means of the file, and it should be able to run on CSV files which don’t fit into memory. Needless to say, it also needs to provide correct results, meaning that it needs to be numerically stable. The example below will only work for small data sets:

CSV_Col_Means <- function(fname){
  dat <- read.csv(fname)
  apply(dat,2,mean)
}
CSV_Col_Means("test.csv")
##        X1        X2        X3        X4        X5        X6        X7 
##  27.64164  50.47091  96.22125 124.77297 184.80935 130.22971  18.16479 
##        X8        X9       X10 
## 929.07646  63.81552  60.78839

One final tip: if you’re generating large files to test your code against, you’ll find that creating them on your H drive (or any other network drive) is far slower than creating them on a local drive. This is due to the simple fact that data on network drives has to be transmitted over… a network. Regardless of how fast the campus intranet is, it’s slower than the connection between the motherboard/CPU in a workstation, and a hard drive.

The interface over which data is transmitted to a hard drive is most commonly a SATA interface, which can handle 6Gbit per second, or 6/8 = 0.75 GB per second. The actual speed of the hard drive is slower than this, having a sequential (meaning in-order, rather than spread out over the whole disk) write speed of something like 150 MB/second. The network speed is substantially slower than this, creating a bottleneck.

With that being said, please don’t leave large files sitting around on random CPH workstations. Delete the files when you’re done with them.

Submission Notes

install.packages("some_magical_R_package", 
                 repos = "http://cran.us.r-project.org")