R script (detailed explanation).docx