moving lecture8 to R programs