I have a custom function that looks like:
f <- function(case, data, variance, limit) {
data <- subset(data, pid != case$pid)
# do some stuff
out <- data.frame(
pid=case$pid,
comp=data$pid
)
return(data.frame(out[with(out, order(--distances)), ][1:limit,], rank=0:(limit-1)))
# distances being a matrix
}
It's applied over a data frame in plyr
like...
adply(subset(data$pid == 123), 1, f, data=data, variance=var(data.frame(data$foo)), limit=5)
I can't figure out how to migrate this to dplyr
using (I guess) mutate()
. How can I pass the current row of the data frame? I tried this..
subset(data, pid==123) %>% mutate(foo=f(data=data, variance=var(data.frame(data$foo)), limit=5))
But the error is that case
is never passed... totally confused.