How extract values of a data.table based on multiple conditions?
I need a function that returns a value of a column data.table based on two other column values:
require(data.table)
dt <- data.table(
"base" = c("of", "of", "of", "lead and background vocals", "save thou me from", "silent in the face"),
"prediction" = c("the", "set", "course", "from", "the", "of"),
"count" = c(258586, 246646, 137533, 4, 4, 4)
)
> dt
# base prediction count
#1: of the 258586
#2: of set 246646
#3: of course 137533
#4: lead and background vocals from 4
#5: save thou me from the 4
#6: silent in the face of 4
# the function needs to return the "prediction" value based on the max "count" value for the input "base" value.
# giving the input "of" to function:
> prediction("of")
# the desired output is:
> "the"
# or:
> prediction("save thou me from")
> "the"