How to write a function

I have a model below that I am trying to write a function in R for.

I Interpret that you are asking to be pointed to the ifelse function that supports vectorised conditional assignment.