Another alternative is to to use do.call
with a call
to construct the call in an environment where "sdate" and "edate" are visible:
f <- function(df, sdate, edate) {
df <- do.call("ddply",list(df, "group", transform,
year = call("year",x= seq(ymd(sdate),
to = ymd(edate), by = "year"))))
return(df)
}
f(df, "1945-1-1", "2013-1-1")
group year
1 1 1945
2 1 1946
3 1 1947
4 1 1948
5 1 1949
6 1 1950
7 1 1951
8 1 1952
EDIT
The plyr-style solution(Simpler)
You should use plyr::here
:
This function captures the current context, making it easier to use
**ply with functions that do special evaluation and need access to the environment where ddply was called from.
f <- function(df, sdate, edate) {
ddply(df, .(group), plyr::here(transform),
year = year(seq(ymd(sdate), ymd(edate), by = "year")))
}