Use sapply instead of for-loop