combination of code. Final r script to modify