Simplify code by using outer() instead of tapply()