When you use summarise , plyr doesn't seem to “see” the function declared in the global environment before checking the function in base :
We can verify this with the convenient Hadley pryr . You can install it with the following commands:
library(devtools) install_github("pryr") require(pryr) require(plyr) c <- ddply(a, .(cat), summarise, print(where("mode")))
In principle, it does not read / does not know / does not see your mode function. There are two alternatives. The first is what @AnandaMahto suggested, and I would do the same and advise you to stick with that. Another alternative is to not use summarise and call it with function(.) So that the mode function is visible in your global environment.
c <- ddply(a, .(cat), function(x) mode(x$levels)) # cat V1 # 1 1 6 # 2 2 5 # 3 3 9
Why does it work?
c <- ddply(a, .(cat), function(x) print(where("mode")))
Because, as you see above, it reads your function, which is in the global environment .
> mode
Unlike:
> base::mode # base mode function # function (x) # { # some lines of code to compute mode # } # <bytecode: 0x7fa2f2bff878> # <environment: namespace:base>
Here's an awesome wiki on Hadley's environments if you are interested in giving it a read / explore further.
source share