今天看到一个很好的tutorial,学习了之后,精简地写出了以下的两个function。前一个是从基本思想出发来写,后一个是对前一个的优化版本。后者在处理数量很大的CSV文件操作时会显出优势。
weightmedian.R:
weightmedian <- function(directory, day) { files_full <- list.files(directory, full.names = TRUE) dat <- data.frame() for (i in 1:5) { dat <- rbind(dat, read.csv(files_full[i])) } dat_day <- dat[which(dat[, "Day"] == day), ] median(dat_day$Weight, na.rm = TRUE) }
optiweightmedian <- function(directory, day) { files_full <- list.files(directory, full.names = TRUE) tmp <- lapply(files_full, read.csv) dat <- do.call(rbind, tmp) dat_day <- dat[which(dat[, "Day"] == day), ] median(dat_day$Weight, na.rm = TRUE) }
|
代码虽短,却足以显示出R语言的简洁高效。简单之中,往往蕴藏着大智慧。