modifed R munging script