Use a pen and paper, write down what you want to do and code it out. Create a function which returns a vector. In the function create a new vector. Add a sensible loop. Add your conditions. Save the values into new vector correctly. Return this new vector.