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
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))