Hi I want to avoid using loops and so want to use something from plyr to help solve my problem.
I would like to create a function that gets the sum of a specifically chosen column for each factor from a dataframe.
So if we have the following example data…
df <- data.frame(cbind(x=rnorm(100),y=rnorm(100),z=rnorm(100),f=sample(1:10,100, replace=TRUE)))
df$f <- as.factor(df$f)
i.e. I would like something like:
foo <- function(df.obj,colname){
some code
}
where the df.obj would be the df variable above and the colname argument could be any of x,y or z.
and I would like the output/result of the function to have a column of the unique factors (in the above case 1:10) and the sums of the values in column x for each factor.
I expect that the solution to be quite simple and would probably be using ddply or summarise somehow but can’t work out how to do it so that i can have the column name as an argument.
Thanks
Is this what you’re after?
Edit: correct answer as supplied by TS: