Edit: up front, efficiency is a relative term. Is there something wrong with your current method? This answer was written on the premise that you do things with multiple datasets (even if subsets of the same original set), but since then it seems that working with all data in a list
is not tenable (due to data size). This last constraint negates the advantage of working with a list-of-frames, but I'll keep the answer here for the record.
I suggest dealing with list
s (ref: How do I make a list of data frames?). Furthermore, it might be done with mapply
.
First let's make a vector of the "breaks" (as you did):
brks <- seq(0L, 45000L, by=15000L)
brks
# [1] 0 15000 30000 45000
("How" you generate it is not critical, just that you have a vector that includes each of the break-points, including as well the first and last.) We'll be using "all but the last" and "all but the first" in two separate vectors, so you can see that we'll be spanning the first element of each, then the second element of each, etc:
brks[-length(brks)]
# [1] 0 15000 30000
brks[-1]
# [1] 15000 30000 45000
This isn't technically a requirement, though. My assumption is that your ranges should be contiguous, so using a vector to do with will guarantee continuity and safe-guard against updating one vector and not the other. You can certainly do (instead) two independent vectors.
mapply
is a "zipper"-like function, where mapply(fun, 1:3, 11:13, SIMPLIFY=FALSE)
is effectively list(fun(1,11), fun(2,12), fun(3,13))
.
ret <- mapply(function(a,b) c2d(fdat.s1, a, b),
brks[-length(brks)], brks[-1],
SIMPLIFY=FALSE)
(SIMPLIFY=FALSE
keeps it as a list
. So many of the *apply
functions like reducing things to vectors or matrices if given the chance, which can be problematic if that is not what you are expecting. To further complicate it, we see sapply(..., simplify=FALSE)
and mapply(..., SIMPLIFY=FALSE)
, so slightly-different options for each.)
At this point, you can loop over the data and do whatever you need, including:
mapply(function(dat, nm) save(dat, file=nm),
ret, sprintf("../RESULTS/nl_mod_df_1_%d.rda", brks[-length(brks)]))
Caveat emptor: untested, since I don't know what c2d
is and I don't have your data. The concept is solid enough, I believe.