Loop with conditional using object known only after apply - oh my!