I am having a similar problem to the one described in this post, where I want to apply a function to 2 columns grouped by each level of a third column. I have tried both solutions presented in this post, and neither worked correctly.
Here are the first 15 lines of my data frame CMdata:
Interval NASC Depth_mean Form
1 0 2.976 0
1 0 4.992 0
1 0 7.008 0
1 0 9.024 0
1 0 11.04 0
1 0 12.96 0
1 0 14.976 0
2 0 2.976 0
2 0 4.992 0
2 0 7.008 0
2 0 9.024 0
2 0 11.04 0
2 0 12.96 0
2 0 14.976 0
3 0 2.976 0
For each Interval, I want to take the sum of Form divided by the sum of NASC (Note: only the beginning of the NASC and Form columns are 0s.) To do this I first created a simple function to do the summing and dividing:
CMfunc <- function(arg1,arg2){
sum(arg1)/sum(arg2)
}
And now I want to apply this to each Interval, 1 through 2104. I tried the solutions presented in the above post, like so:
dt = data.table(CMdata)
CM <- dt[,CMfunc(CMdata$form,CMdata$NASC),by=CMdata$Interval]
But the output is just 23.49
for every row in the new column created. I've also tried various looping functions (tapply
, etc.) without success. I feel like this should be very easy but I continue to get errors or incorrect output.