用户自定义函数


2022年4月13日, Learn eTutorial
2564

除了内置函数,R 编程允许用户创建自己的函数以满足其编程需求。创建用户自定义函数时遵循的语法与我们在主题 “如何在 R 中创建函数?” 中讨论的基本语法相同。

R 编程语言提供了不同的方法来从程序的任何位置调用用户创建的函数。在 R 中调用函数的不同方法有:

  • 在 R 中无参数函数调用
  • 在 R 中带参数函数调用
  • 带默认参数函数调用

在接下来的部分中,您将探索在 R 编程语言中处理函数调用的不同方法。

如何在 R 中调用函数?

在 R 编程语言中,函数可以带参数、不带参数或带默认参数调用。让我们逐个案例进行了解,以理解如何在实践中使用它们。这些被归类为用户自定义函数。

1. 在 R 中无参数函数调用

在不提供任何参数的情况下创建函数,代码的执行决定了结果。

函数可以在不传递任何参数的情况下通过括号 () 进行调用。函数() 括号将为空。

语法


function_name = function(){
     #body_of_function                                                     
}

让我们通过一些实际例子来学习

函数 ls() 的括号内没有任何参数。ls() 的执行提供了 R 环境中的所有当前对象。下面的代码片段显示了在当前使用 RStudio 执行 ls() 后它返回的内容。

ls fn snippet

它返回在早期程序中创建的对象列表,例如 add、circle、Mul、r 等。因此,在程序中执行某些场景时,需要不带参数的函数。


#Create a function website 
website = function(){
  print("Learn eTutorials")
}
#call the function 
website()
website()
website()

考虑另一个示例,用于在函数中不带任何参数显示单个语句。

在此示例中,名为 website() 的函数被执行 3 次,因为它被调用了 3 次。请记住,没有参数通过 website() 函数传递。让我们检查输出


[1] "Learn eTutorials"
[1] "Learn eTutorials"
[1] "Learn eTutorials"

另一个创建用户自定义函数以计算圆面积的示例。


#function call without an argument
circle = function(){
  for(r in 1:5){
  area = pi *r*r   #pi is constant for 3.14
  print(area)
  }
}

circle()    #No arguments are passed inside while invoking function circle()

函数 circle() 生成的输出显示,函数体执行了 5 次,因为函数内部构造了一个 for 循环,以满足函数应该执行 5 次,范围从 1 到 5 的条件。当变量 r=1 时,它提供 3.141593;当 r=2 时,它提供 12.56637,依此类推,直到 r 满足计算圆面积的条件。


[1] 3.141593
[1] 12.56637
[1] 28.27433
[1] 50.26548
[1] 78.53982

2. 在 R 中带参数函数调用

函数通过传递一些参数来调用函数体的执行,以提供所需的输出。

在给定的示例中,将等于 2 的参数 r 传递给 circle() 函数以查找圆的面积。

它遵循的语法是创建函数的基本结构。

语法


Function_name = function(arguments){
     #body_of_function                                                     
}

考虑一个简单的例子,函数 pow 中只有一个参数。


pow = function(a){
  power = a*a
  return(power)
}

pow(4)

函数 pow(4) 定义代码以查找 4 的幂,即 16。生成的输出是


[1] 16

考虑另一个用户定义的函数,它接受参数 r 来计算圆的面积。


#function call with an argument
circle = function(r){
   {
    area = pi *r*r   #pi is constant for 3.14
    
    cat("The area of the circle when r is ", paste(r)," is”, area)
  }
}

circle(2)    #argument  passed inside while invoking function circle()

输出是


The area of the circle when r is  2  is 12.56637

3. 带默认参数的函数调用

一些函数可以有默认参数,这些参数在程序中创建或定义函数时直接传递。

语法与带参数的函数类似,不同之处在于参数的值在创建函数时默认提供。


#function call with default argument
circle = function(r=2){                 
  {
    area = pi *r*r   #pi is constant for 3.14
    cat("The area of the circle when r is ",paste(r)," is",area)
  }
}

circle()   #function call without giving an argument

circle(1)  #function call with argument

输出


The area of the circle when r is  2  is 12.56637
> circle(1)
The area of the circle when r is  1  is 3.141593

R 中的链式函数或嵌套函数是什么?

一个函数嵌套在另一个函数调用中是 R 中的链式函数。它也可以称为嵌套函数调用。一个函数内部包含另一个函数。外部函数将内部函数的结果作为其参数。

语法


Function_name = function(function as arguments){
     #body_of_function                                                     
}

考虑一个示例,我们定义两个函数来将华氏度转换为摄氏度 (fahrenheit_to_celsius) 和将摄氏度转换为开尔文 (celsius_to_kelvin)


fahrenheit_to_celsius <- function(temp_F) {
  temp_C <- (temp_F - 32) * 5 / 9
  return(temp_C)
}

# boiling point of water
fahrenheit_to_celsius(212)

产生的输出是


[1] 100

接下来,让我们看看摄氏到开尔文的转换函数


celsius_to_kelvin <- function(temp_C) {
  temp_K <- temp_C + 273.15
  return(temp_K)
}

# boiling point of water in Kelvin
celsius_to_kelvin(100)


输出


[1] 373.15

如何将华氏度转换为开尔文?

它通过将函数名分配给不同的变量,在函数内部直接将华氏度转换为开尔文。


fahrenheit_to_kelvin <- function(temp_F) {
  temp_C <- fahrenheit_to_celsius(temp_F)
  temp_K <- celsius_to_kelvin(temp_C)
  return(temp_K)
}

# boiling point of water in Kelvin
fahrenheit_to_kelvin(212)


输出显示了从华氏度到开尔文的转换结果。


[1] 373.15

通过使用 R 中的嵌套函数,相同的代码块可以像所示一样在一行中执行


# boiling point of water in Fahrenheit
celsius_to_kelvin(fahrenheit_to_celsius(212))

生成的输出是


[1] 373.15