I'm not that good at reading R. But if the Julia code is similar, then this code is type unstable, sometimes returning an Int, sometimes a Float. That harms performance.
Generally, it looks like a function where Julia could have a significant performance advantage.