Reputation: 993
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:
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
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