Revised R Code