tweaked R code for post