How can I parameterize the function call to aggregate() below with colx (a
character string) in the following code?   That is, replace both if
statements with aaa <- aggregate(colx_something, data=tidy[grpyrs,],sum).
I've tried a couple of ideas for "colx_something" but get an R-error each
time.  Been using R for 5-6 months and want to expand my knowledge.

explain_causes <- function(spread, colx, peakyears) {
    for(i in peakyears) {
        grpyrs <- tidy$year >= (i-spread) & tidy$year <= (i+spread)
        if(colx == "harm")
            aaa <- aggregate(harm ~ EVTYPE, data=tidy[grpyrs,],sum)
        if(colx == "damage")
            aaa <- aggregate(damage ~ EVTYPE, data=tidy[grpyrs,],sum)
        .
        .
        .
    }
}

# "harm" is a column var in tidy dataframe
explain_causes(0,"harm",c(1953,1965,1974,1979,1984,1995,1998,2011))

# "damage" is a column var in tidy dataframe
explain_causes(0,"damage",c(2005,2006))

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to