I am trying to write a function that "variabilizes" the ddply call:
december <- ddply(adk47, .(PeakName, Elevation), summarize,
needThese=if(sum(dec) == 0) "needThis"
else character(0), .progress='text')
Where there are 3 letter column names for each month in the df. I am trying to write the function as:
need.fr.month <- function(df, monthCol) {
needThese <- ddply(df, .(PeakName, Elevation),
summarize,
needThese=if(sum(monthCol) == 0)
"needThis" else character(0)
)
return(needThese)
}
but when I call this with
need.fr.month(adk47, oct)
or with
need.fr.month(adk47, "oct")
I get the these error messages:
Error in eval(expr, envir, enclos) : object 'monthCol' not found
or
Error in sum("monthCol") : invalid 'type' (character) of argument
I know that I am not getting something very basic, but I don't know what.
I am using this DF to practice writing R functions. My other functions have gone fairly well; however, this is the first function in which I am trying to variabilize a df column.
Help would be gratefully appreciated.
Here is a Reproduceable Example for a subset of the data
PeakName Elevation jul aug sep oct nov dec
Algonquin 5114 0 0 1 0 0 0
Algonquin 5114 0 0 0 0 0 0
Algonquin 5114 0 0 0 1 0 0
Algonquin 5114 1 0 0 0 0 0
Allen 4340 0 0 0 0 0 0
Allen 4340 0 0 0 0 0 0
Allen 4340 0 0 1 0 0 0
Allen 4340 1 0 0 0 0 0
Allen 4340 0 0 0 0 1 0
Armstrong 4400 0 0 0 0 0 0
Armstrong 4400 0 0 0 0 0 0
Armstrong 4400 0 0 0 0 0 0
Armstrong 4400 0 0 0 0 0 0
Armstrong 4400 0 0 0 0 1 0
Armstrong 4400 0 0 0 0 0 0
Armstrong 4400 0 0 0 1 0 0
Basin 4827 1 0 0 0 0 0
Basin 4827 0 0 0 0 0 0
Basin 4827 0 0 0 0 0 0
Basin 4827 0 0 0 0 0 0
Basin 4827 0 0 0 0 0 0
Basin 4827 0 0 0 0 0 0
Basin 4827 0 0 0 0 1 0
Big.Slide 4240 0 0 0 0 0 0
Big.Slide 4240 0 0 0 1 0 0
Big.Slide 4240 0 0 0 0 0 0
Big.Slide 4240 0 0 1 0 0 0
Big.Slide 4240 0 0 0 0 0 0
Big.Slide 4240 0 0 0 0 0 0
Big.Slide 4240 0 0 0 0 0 0
Big.Slide 4240 1 0 0 0 0 0
I hope this suffices. Clearly this is a subset of the data. The form is that each "hike" has one line with the months columns (here truncated to July thru December) indicating a "1" for one month and a zero for the other 11.
Thanks
Wayne