How to get the mode of a group in summarize in R
I use this approach:
df <- data.frame(groups = c("A", "A", "A", "B", "B", "C", "C", "C", "D"), nums = c("1", "2", "1", "2", "3", "4", "5", "5", "1"))
which looks like:
groups nums
A 1
A 2
A 1
B 2
B 3
C 4
C 5
C 5
D 1
Then I define:
mode <- function(codes){
which.max(tabulate(codes))
}
and do the following:
mds <- df %>%
group_by(groups) %>%
summarise(mode = mode(nums))
giving:
groups mode
A 1
B 2
C 5
D 1
You need to make a couple of changes to your code for mlv to work.
- the method (mfv) has to be within quotes ('mfv'). That is what is causing your error.
- After you do that, since mlv returns a list, you have to feed one value to summarise(). Assuming that you want the mode ('M'), you pick that element from the list.
Try:
dataSummary <- dataObs %>%
group_by(ParNonPar, CPTCode) %>%
summarise(mean = mean(net_paid),
meadian=median(net_paid),
mode = mlv(net_paid, method='mfv')[['M']],
total = sum(net_paid))
to get:
> dataSummary
Source: local data frame [3 x 6]
Groups: ParNonPar
ParNonPar CPTCode mean meadian mode total
1 N 104 639.7111 893.00 622.7333 5757.40
2 Y 100 0.0000 0.00 0.0000 0.00
3 Y 103 740.2800 740.28 740.2800 740.28
Hope that helps you move forward.