[R] parameterization question

Dale Garman garman.dale at gmail.com
Fri Sep 5 19:57:59 CEST 2014

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

# "damage" is a column var in tidy dataframe

	[[alternative HTML version deleted]]

More information about the R-help mailing list