R function asked in the exercise