Updated with R codes