I need to calculate the mode of an identity number for each group of ages. Let\'s suposse the following table:
library(data.table)
DT = data.table(age=c(12,1
One approach:
> myfun <- function(x) unique(x)[which.max(table(x))]
> DT[ , moda := myfun(number), by = age]
> DT
age v number moda
1: 12 -0.9740026 122 122
2: 12 0.6893727 125 122
3: 3 -0.9558391 5 5
4: 3 -1.2317071 5 5
5: 12 -0.9568919 122 122
You can use dplyr
for this:
library(dplyr)
modes_by_age <- summarise(group_by(DT, age), group_mode = g(number))
inner_join(DT, modes_by_age)
This gives your desired output:
Source: local data table [5 x 4]
age v number group_mode
1 3 0.5524352 5 5
2 3 0.2869912 5 5
3 12 0.8987475 122 122
4 12 0.9740715 125 122
5 12 2.5058450 122 122
modef <- function(V)
{
k = 1
prev='xxxx'
max_value = 0
for (i in V)
{
if (prev == i)
{
k = k+1
}
else
{
if (k > max_value)
{
MODE_CALC = data.frame(
number = c(prev) ,
occurence = c(k) )
max_value = k
k = 1
}
k = 1
}
prev = i
}
print(MODE_CALC$number)
}
V = c(11, 11, 11, 11, 12, 12, 2, 2, 2, 2, 2, 2, 14, 14, 14, 15, 16, 17, 17, 17 ,17 ,
17, 18, 19)
modef(sort(V))
Here's a base R solution. You could compute the mode for each group and then merge with your original data:
merge(DT, setNames(aggregate(number~age, data=DT, g), c("age", "moda")), by="age")
# age v number moda
# 1: 3 1.7148357 5 5
# 2: 3 0.9504811 5 5
# 3: 12 -0.7648237 122 122
# 4: 12 0.9011115 125 122
# 5: 12 -0.8718779 122 122
There may be a data table-specific approach, but this would work even if DT
were a data frame.