-2

This is apparently a simple problem but I can't figure out what function to use. Following are sample data:

gg <- data.frame(ID = c(15,15,15,16,16,16, 16,17,17,17),
                 ADO = c(rep("T1", 4), rep("T2", 2), rep("T3", 4)))

The "ID" is the label or category of a particular "ADO". It should be unique to each ADO. But in this case it is not:

> table(gg$ID, gg$ADO)

     T1 T2 T3
  15  3  0  0
  16  1  2  1
  17  0  0  3  

I want to assign the most frequent ID to a particular ADO. So, my desired output is:

   ID ADO
1  15  T1
2  15  T1
3  15  T1
4  16  T2
5  16  T2
6  16  T2
7  16  T2
8  17  T3
9  17  T3
10 17  T3  

Please guide me what function can I use to fix this?

umair durrani
  • 5,597
  • 8
  • 45
  • 85
  • 1
    http://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode – Nate Nov 25 '16 at 18:19
  • @NathanDay Thanks a lot! – umair durrani Nov 25 '16 at 18:24
  • 1
    One note. You can create your ADO variable more succinctly by feeding `rep` a vector in the second argument: `c(rep("T1", 4), rep("T2", 2), rep("T3", 4))` would become `rep(c("T1", "T2", "T3"), c(4, 2, 4))`. – lmo Nov 25 '16 at 19:32

1 Answers1

0

This shows one way of finding the first mode of ADO:

do.call("rbind", by(gg, gg$ID, function(x) { tbl <- table(x$ADO); x$ADO <- names(sort(tbl, decreasing = TRUE)[1]); x}))
#       ID ADO
# 15.1  15  T1
# 15.2  15  T1
# 15.3  15  T1
# 16.4  16  T2
# 16.5  16  T2
# 16.6  16  T2
# 16.7  16  T2
# 17.8  17  T3
# 17.9  17  T3
# 17.10 17  T3
r2evans
  • 141,215
  • 6
  • 77
  • 149