I am using ddply to aggregate and summarize data frame variables, and I am interested in looping through my data frame's list to create the new variables.
new.data <- ddply(old.data,
c("factor", "factor2"),
function(df)
c(a11_a10 = CustomFunction(df$a11_a10),
a12_a11 = CustomFunction(df$a12_a11),
a13_a12 = CustomFunction(df$a13_a12),
...
...
...))
Is there a way for me to insert a loop in ddply so that I can avoid writing each new summary variable out, e.g.
for (i in 11:n) {
paste("a", i, "_a", i - 1) = CustomFunction(..... )
}
I know that this is not how it would actually be done, but I just wanted to show how I'd conceptualize it. Is there a way to do this in the function I call in ddply, or via a list?
UPDATE: Because I'm a new user, I can't post an answer to my own question:
My answer involves ideas from Nick's answer and Ista's comment:
func <- function(old.data, min, max, gap) {
varrange <- min:max
usenames <- paste("a", varrange, "_a", varrange - gap, sep="")
new.data <- ddply(old.data,
.(factor, factor2),
colwise(CustomFunction, c(usenames)))
}
transform()
orsummarize()
. the help page forsummarize
shows some good examples.?colwise
(see the examples for use with ddply).