Ayelavan
Ayelavan

Reputation: 993

Aggregate data with custom function

I have a data.table which looks this:

Category   Sub-Category     Status
   A            1            Draft
   A            2            Draft
   A            3            Draft
   A            4            Draft
   A            5            Draft
   A            6            Draft
   B            1            Final
   B            2            Draft
   B            3            Final
   B            4            Final
   C            1            Final
   C            2            Final
   C            3            Final
   C            4            Final
   C            5            Final

I want to aggregate this data by Category so that:

  1. the status is "Draft" if all the sub-categories are a status of "Draft"
  2. the status is "Hybrid" if the sub-categories have status's that are a mix of "Draft" and "Final"
  3. the status is "Final" if the all the sub-categories are a status of "Final"

The final output will look like this:

Category   Status
   A       Draft
   B       Hybrid
   C       Final

I know that if the function I was applying was sum, I could do something like this:

df <- aggregate(Status ~ Category, data = df, sum)

The issue for me is that this would require a custom function of some sort

Upvotes: 0

Views: 1528

Answers (1)

LuckySeedling
LuckySeedling

Reputation: 425

Here you go, try this..

customFunc <- function(x){

    if (length(unique(as.character(x))) == 2){
        return('Hybrid')
    }

    else {
        return(unique(as.character(x)))
    }

}

df <- aggregate(Status ~ Category, data = df, customFunc)

Upvotes: 3

Related Questions