Aryan PrajapatKnowledge Contributor
How to create a user-defined function in R?
How to create a user-defined function in R?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
To create a user-defined function in R, we use the keyword function and the following syntax:
function_name <- function(parameters){
function body
}
Function name—the name of the function object that will be used for calling the function after its definition.
Function parameters—the variables separated with a comma and placed inside the parentheses that will be set to actual argument values each time we call the function.
Function body—a chunk of code in the curly brackets containing the operations to be performed in a predefined order on the input arguments each time we call the function. Usually, the function body contains the return() statement (or statements) that returns the function output, or the print() statement (or statements) to print the output.