R 简明教程
R - Functions
函数是一组陈述,为了执行特定的任务而被组织在一起。R 具有大量的内置函数并且用户可以创建自己的函数。
A function is a set of statements organized together to perform a specific task. R has a large number of in-built functions and the user can create their own functions.
在 R 中,函数是一个对象,因此 R 解释器能够将控制权传递给函数,连同对于函数完成操作可能必要的参数。
In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions.
函数执行其任务并将其控制权以及存储在其他对象中的任何结果返回给解释器。
The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects.
Function Definition
R 函数是通过使用关键字 function 创建的。R 函数定义的基本语法如下−
An R function is created by using the keyword function. The basic syntax of an R function definition is as follows −
function_name <- function(arg_1, arg_2, ...) {
Function body
}
Function Components
函数的不同部分是−
The different parts of a function are −
-
Function Name − This is the actual name of the function. It is stored in R environment as an object with this name.
-
Arguments − An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values.
-
Function Body − The function body contains a collection of statements that defines what the function does.
-
Return Value − The return value of a function is the last expression in the function body to be evaluated.
R 有许多 in-built 函数,可在程序中直接调用,而无需先定义它们。我们还可以创建和使用我们自己的函数,称为 user defined 函数。
R has many in-built functions which can be directly called in the program without defining them first. We can also create and use our own functions referred as user defined functions.
Built-in Function
内置函数的简单示例包括 seq() 、 mean() 、 max() 、 sum(x) 和 paste(…) 等。它们由用户编写的程序直接调用。你可以参考 most widely used R functions.
Simple examples of in-built functions are seq(), mean(), max(), sum(x) and paste(…) etc. They are directly called by user written programs. You can refer most widely used R functions.
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))
# Find mean of numbers from 25 to 82.
print(mean(25:82))
# Find sum of numbers frm 41 to 68.
print(sum(41:68))
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526
User-defined Function
我们可以在 R 中创建用户定义函数。它们取决于用户的要求,并且在创建后可以像内置函数一样使用它们。以下是创建和使用函数的一个示例。
We can create user-defined functions in R. They are specific to what a user wants and once created they can be used like the built-in functions. Below is an example of how a function is created and used.
# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
Calling a Function
# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
# Call the function new.function supplying 6 as an argument.
new.function(6)
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
Calling a Function without an Argument
# Create a function without an argument.
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
# Call the function without supplying an argument.
new.function()
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
Calling a Function with Argument Values (by position and by name)
可以按函数中定义的相同顺序提供函数调用的参数,也可以按不同的顺序提供参数,但是被分配给参数的名称。
The arguments to a function call can be supplied in the same sequence as defined in the function or they can be supplied in a different sequence but assigned to the names of the arguments.
# Create a function with arguments.
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
# Call the function by position of arguments.
new.function(5,3,11)
# Call the function by names of the arguments.
new.function(a = 11, b = 5, c = 3)
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 26
[1] 58
Calling a Function with Default Argument
我们可以定义函数定义中的参数的值,并且在不提供任何参数的情况下调用函数以获取默认结果。但是,我们也可以通过提供参数的新值并获得非默认结果来调用此类函数。
We can define the value of the arguments in the function definition and call the function without supplying any argument to get the default result. But we can also call such functions by supplying new values of the argument and get non default result.
# Create a function with arguments.
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}
# Call the function without giving any argument.
new.function()
# Call the function with giving new values of the argument.
new.function(9,5)
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 18
[1] 45
Lazy Evaluation of Function
对函数的参数进行惰性求值,这意味着只有函数体需要时才会评估它们。
Arguments to functions are evaluated lazily, which means so they are evaluated only when needed by the function body.
# Create a function with arguments.
new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}
# Evaluate the function without supplying one of the arguments.
new.function(6)
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default