问题
This question is a follow-up to Count days per year.
I did what Dirk suggested with a huge data.frame. My commands look like this:
dateSeq <- function(df) {
res <- seq(as.Date(df["begin"]), as.Date(df["end"]), by = "1 day")
format(res, "%Y")
}
dataFrame$seq <- apply(dataFrame, 1, dateSeq)
dataFrame_years <- do.call("c", dataFrame[["seq"]])
rm(dataFrame)
gc()
gc()
dataFrame_tab <- table(dataFrame_years)
Now, these commands fill up my 8 GB Ram and 2 GB swap space. In the mean time my processor is bored having a processor load of maybe 15 %.
Besides, it takes ages for my computer to fulfill my "desires". Can I shift some of the work to the CPU and unburden my Ram a bit?
回答1:
Indeed, the referred solution is uneccessary memory hungry. Try this:
begin <- as.POSIXlt("2007-05-20", tz = "GMT")
end <- as.POSIXlt("2010-06-13", tz = "GMT")
year <- seq(begin$year, end$year) + 1900
year.begin <- as.POSIXlt(paste(year, "01", "01", sep="-"), tz="GMT")
year.begin[1] <- begin
year.end <- as.POSIXlt(paste(year, "12", "31", sep="-"), tz="GMT")
year.end[length(year.end)] <- end
days <- as.numeric(year.end - year.begin) + 1
cbind(year, days)
来源:https://stackoverflow.com/questions/9534351/make-this-process-more-processor-intensive-and-less-memory-intensive