My mistake!: Needs if then else, not ifelse -- otherwise only the first line of code is written.  

I messed up by using ifelse() when I should have used if then else.  The old code just wrote the first line, not all the lines of code.