Instruction: Explain the process of writing and optimizing a custom function in R for a specific data analysis task.
Context: This question tests the candidate's ability to extend R's functionality through custom functions, a key skill for advanced data analysis.