0

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.

Community
  • 1
  • 1
H913
  • 1
  • 1
  • perhaps `setDT(dt)[,newcol := sum(Form) / sum(NASC), by = Interval]` ? – mtoto Feb 10 '16 at 15:41
  • That is producing correct answers in the new column, thank you!! It does apply it to every single row though, which doesn't really matter for what I need, but others may not want that. – H913 Feb 10 '16 at 18:33
  • If you just want summary table try `setDT(dt)[,sum(Form) / sum(NASC), by = Interval]` – mtoto Feb 10 '16 at 18:37

0 Answers0