R 函数

R 函数介绍

函数只是一个代码块,你可以从程序的任何部分调用并运行它。它们用于将代码分解为简单的部分并避免重复的代码。

你可以借助参数将数据传递给函数,并返回一些其他数据作为结果。你可以使用 function() 保留关键字在 R 中创建函数。语法如下:

func_name <- function (parameters) {
statement
}

这里,func_name 是函数的名称。例如:

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

这里,我们定义了一个名为 power 的函数,它接受两个参数 - ab。在函数内部,我们包含了一段代码来打印 ab 次幂的值。


调用函数

定义函数后,你可以使用函数名和参数来调用函数。例如:

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(2, 3)

输出

[1] "a raised to the power b is:  8"

这里,我们用两个参数 - 23 调用了函数。这将打印 23 次幂的值,即 8

实际函数中使用的参数称为形式参数。它们也称为形参。调用函数时传递给函数的值称为实际参数。


命名参数

在上述 power() 函数的函数调用中,函数调用时传递的参数顺序必须与函数声明时传递的参数顺序相同。

这意味着当我们调用 power(2, 3) 时,值 2 被赋给 a,值 3 被赋给 b。如果你想改变参数传递的顺序,你可以使用命名参数。例如:

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(b=3, a=2)

输出

[1] "a raised to the power b is:  8"

这里,无论你在函数调用时传递的参数顺序如何,结果都是相同的。

你也可以混合使用命名参数和未命名参数。例如:

# define a function to compute power
power <- function(a, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(b=3, 2)

输出

[1] "a raised to the power b is:  8"

默认参数值

你可以为函数分配默认参数值。为此,你可以在函数定义期间为函数参数指定适当的值。

当你调用没有参数的函数时,将使用默认值。例如:

# define a function to compute power
power <- function(a = 2, b) {
    print(paste("a raised to the power b is: ", a^b))
}

# call the power function with arguments
power(2, 3)

# call function with default arguments
power(b=3)

输出

[1] "a raised to the power b is:  8"
[1] "a raised to the power b is:  8"

这里,在第二次调用 power() 函数时,我们只将 b 参数指定为命名参数。在这种情况下,它使用函数定义中为 a 提供的默认值。


返回值

你可以使用 return() 关键字从函数返回数值。例如:

# define a function to compute power
power <- function(a, b) {
    return (a^b)
}

# call the power function with arguments
print(paste("a raised to the power b is: ", power(2, 3)))

输出

[1] "a raised to the power b is:  8"

这里,我们没有在函数内部打印结果,而是返回了 a^b。当我们用参数调用 power() 函数时,结果被返回,可以在调用时打印出来。


嵌套函数

在 R 中,你可以通过 2 种不同的方式创建嵌套函数。

  • 通过在另一个函数调用内部调用函数。
  • 通过在另一个函数内部编写函数。

示例 1:在另一个函数调用内部调用函数

考虑下面的例子来创建一个函数来添加两个数字。

# define a function to compute addition
add <- function(a, b) {
    return (a + b)
}

# nested call of the add function 
print(add(add(1, 2), add(3, 4)))

输出

[1] 10

这里,我们创建了一个名为 add() 的函数来添加两个数字。但在函数调用期间,参数是对 add() 函数的调用。

首先,计算 add(1, 2)add(3, 4),并将结果作为参数传递给外部 add() 函数。因此,结果是所有四个数字的和。


示例 2:在另一个函数内部编写函数

让我们通过在另一个函数内部编写函数来创建一个嵌套函数。

# define a function to compute power
power <- function(a) {
    exponent <- function(b) {
        return (a^b)
    }
    return (exponent)
}

# call nested function 
result <- power(2)
print(result(3))

输出

[1] 8

这里,我们不能直接调用 power() 函数,因为 exponent() 函数定义在 power() 函数内部。

因此,我们首先需要用参数 a 调用外部函数并将其设置为一个变量。现在这个变量充当一个函数,我们将下一个参数 b 传递给它。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战