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.
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.70 0.07 0.76
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
## 0.87 0.01 0.90
It’s a little bit faster, and we can make a considerably larger:
system.time(GenerateDataSet2(10000000, 10, batch_size = 1000, "test.csv"))
## user system elapsed
## 90.73 1.56 92.36
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
## 68.32093 41.23393 25.97121 31.71010 123.48192 1929.95212
## X7 X8 X9 X10
## 204.39845 2024.74807 158.57737 1336.73852
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.