edited and tested R file