Search code examples
rloopsaggregateconditional-statementsdcast

Calculate value with two columns based on four conditions in R


I have a large dataset uploaded in r (see below for a short version): I want to calculate a value for each Cruiseid, Samplenr, Species and Age (so based on four conditions):

Cruiseid    Samplenr Species Age Length LK  TNumStat    TNumLK
197502      37        154   0   12,5    2   2,791666667 5,583333
197502      37        154   0   17,5    3   2,166666667 6,5
197502      37        154   2   172,5   34  11,54166667 392,4167
197502      37        154   2   177,5   35  12,0625 422,1875
197502      37        154   2   182,5   36  2,083333333 75
197502      35        154   0   112,5   22  11,85654008 260,8439
197502      35        154   2   197,5   39  2,109704641 82,27848
197502      35        154   2   217,5   43  2,109704641 90,7173
197502      35        154   2   232,5   46  2,109704641 97,04641
197502      36        154   0   12,5    2   4,685314685 9,370629
197502      36        154   2   182,5   36  3,496503497 125,8741
197502      41        154   0   17,5    3   2,260869565 6,782609
197502      41        154   2   202,5   40  4,347826087 173,913
197502      41        154   2   212,5   42  2,173913043 91,30435
197502      41        154   2   242,5   48  2,173913043 104,3478
197503      56        154   0   17,5    3   7,428571429 22,28571
197503      56        154   0   147,5   29  10,30952381 298,9762
197503      56        154   2   172,5   34  13,19047619 448,4762
197503      56        154   2   187,5   37  2,380952381 88,09524
197503      54        154   0   12,5    2   3,35        6,7
197503      54        154   0   157,5   31  12          372
197503      54        154   0   167,5   33  13,25       437,25
197503      54        154   2   172,5   34  13,85       470,9
197503      54        154   2   187,5   37  2,5         92,5
197503      54        154   2   217,5   43  2,5         107,5
197503      53        154   0   12,5    2   2,875536481 5,751073
197503      53        154   0   97,5    19  4,806866953 91,33047
197503      53        154   0   107,5   21  5,622317597 118,0687
197503      53        154   0   142,5   28  8,776824034 245,7511

I want to calcuate:((TNumStat$TNumLK/TNumStat$TNumStat)*0.5+0.25)*10for each Cruiseid, Samplenr, Species and Age.

I have already tried something in a loop construction:

#######################
Cruise <- unique(TNumStat$Cruiseid)
Track <- unique(TNumStat$Samplenr)
#######################
AvrLengthCr <- c()
AvrLengthCr <- rep(NA, length(TNumStat$Species))
#######################
for(j in 1:length(Cruise)){
  t1.ss <- which(TNumStat$Cruiseid ==  Cruise[j])
  ###
  for(i in 1:length(Track)){
    t2.ss <- which(TNumStat$Samplenr[t1.ss] ==  Track[i])
    ###
    AvrLengthCr[t1.ss][t2.ss] <- ((TNumStat$TNumLK[t1.ss][t2.ss]/TNumStat$TNumStat[t1.ss][t2.ss])*0.5+0.25)*10
  }}

But it doesn't seem to work. And I've also been looking at something with dcast:

TNumStat2<-dcast(TNumStat,Cruiseid+Samplenr+Species+Age,formula = (((TNumStat$TNumLK/TNumStat$TNumStat*0.5+0.25)*10) )),na.rm=TRUE)

Non of the options I have tried seem to work, and I dont know how to solve this. Can someone please help me?

Thank you


Solution

  • Good Morning,

    the question is not totally clear in my opinion. But you could try something like (with dplyr)

    sample <- sample %>%
      mutate(calculate = ((TNumLK/TNumStat) * 0.5 + 0.25) * 10) %>%
      group_by(Cruiseid, Samplenr, Species, Age)
    
    summarisedDF <- sample %>%
      summarise(avg.calculate = mean(calculate))