[r] write test and fix functions to pass test