Nick Pietrushka
Nick Pietrushka

Reputation: 135

Apply aggregate to defined function using R

I'm writing an aggregate function using R, and I need to call inside aggregation defined function peakdet_new.

peakdet_new <- function(dmx, v, x = NULL)
{
 ....

  if(nrow(data.frame(list(maxtab = maxtab)))== 1)
  {
    data.frame(DataMatrix=c(unique(dmx)), Status=c("OK"))
  } else
  {
    data.frame(DataMatrix=c(unique(dmx)), Status=c("NOK"))
  }
  #list(maxtab = maxtab) #,mintab = mintab)
  #return(maxtab)
  # if(nrow(data.frame(list(maxtab = maxtab)) >= 1))
  #        {
  #          return(print("NOK"))
  #        } else
  #          {
  #          return(print("OK"))
  #        }

}

What gives me the output:

peakdet_new(DMX_$DataMatrix, DMX_$X, DMX_$Y)
                        DataMatrix Status
1 00003275216311504500-A1820800306    NOK

Here is my table

DataMatrix  X   Y
00003275216311504500-A1812300356    0.23    30000
00003275216311504500-A1812300356    0.23    30483
00003275216311504500-A1829600367    0.41    30000
00003275216311504500-A1829600367    0.42    30483

And the problem is i don't know how to apply peakdet_new to aggregate function, below what i've done

data.agg <- aggregate(DMX_together[c('X', 'Y')], by = DMX_together['DataMatrix'], FUN = peakdet_new(DMX_together$DataMatrix, DMX_together$X, DMX_together$Y))
Error in match.fun(FUN) : 
  'peakdet_new(DMX_together$DataMatrix, DMX_together$X, DMX_together$Y)' is not a function, character or symbol

Without arguments

data.agg <- aggregate(DMX_together[c('X', 'Y')], by = DMX_together['DataMatrix'], FUN = peakdet_new)
Error in FUN(X[[i]], ...) : argument "v" is missing, with no default

I expect the output of aggregate function:

   DataMatrix                         Status
1 00003275216311504500-A1820800306    NOK
2 00003275216311504500-A1829600367    NOK

Upvotes: 0

Views: 86

Answers (1)

Parfait
Parfait

Reputation: 107567

Since you need to run operations on a whole data frame, consider calling peakdet_new with by subsetting by each distinct DataMatrix. On the other hand, aggregate usually expects a unit level vector sliced by the by factors to return an aggregated vector (later binded with grouped factors for a data frame return).

Calling by will run operation on each subsetted data frames to build a list (equal to unique values of subset) where you can then rbind all elements into a single data frame:

df_list <- by(DMX_, DMX_$DataMatrix, function(sub) 
                peakdet_new(sub$DataMatrix, sub$X, sub$Y))

final_df <- do.call(rbind, unname(df_list))

Upvotes: 1

Related Questions