Updated R script with my solution