欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言函數(shù)詳解及實(shí)例用法

 更新時(shí)間:2021年03月28日 15:04:24   作者:w3cschool  
在本篇內(nèi)容里小編給大家?guī)?lái)一篇關(guān)于R語(yǔ)言函數(shù)詳解及實(shí)例用法,有需要的朋友們可以學(xué)習(xí)參考下。

函數(shù)是一組組合在一起以執(zhí)行特定任務(wù)的語(yǔ)句。 R 語(yǔ)言具有大量?jī)?nèi)置函數(shù),用戶可以創(chuàng)建自己的函數(shù)。

在R語(yǔ)言中,函數(shù)是一個(gè)對(duì)象,因此R語(yǔ)言解釋器能夠?qū)⒖刂苽鬟f給函數(shù),以及函數(shù)完成動(dòng)作所需的參數(shù)。

該函數(shù)依次執(zhí)行其任務(wù)并將控制返回到解釋器以及可以存儲(chǔ)在其他對(duì)象中的任何結(jié)果。

函數(shù)定義

使用關(guān)鍵字函數(shù)創(chuàng)建 R 語(yǔ)言的函數(shù)。 R 語(yǔ)言的函數(shù)定義的基本語(yǔ)法如下

function_name <- function(arg_1, arg_2, ...) {
  Function body 
}

函數(shù)組件

函數(shù)的不同部分 -

  • 函數(shù)名稱 -這是函數(shù)的實(shí)際名稱。 它作為具有此名稱的對(duì)象存儲(chǔ)在 R 環(huán)境中。
  • 參數(shù) -參數(shù)是一個(gè)占位符。 當(dāng)函數(shù)被調(diào)用時(shí),你傳遞一個(gè)值到參數(shù)。 參數(shù)是可選的; 也就是說,一個(gè)函數(shù)可能不包含參數(shù)。 參數(shù)也可以有默認(rèn)值。
  • 函數(shù)體 -函數(shù)體包含定義函數(shù)的功能的語(yǔ)句集合。
  • 返回值 -函數(shù)的返回值是要評(píng)估的函數(shù)體中的最后一個(gè)表達(dá)式。

R語(yǔ)言有許多內(nèi)置函數(shù),可以在程序中直接調(diào)用而無(wú)需先定義它們。我們還可以創(chuàng)建和使用我們自己的函數(shù),稱為用戶定義的函數(shù)。

內(nèi)置功能

內(nèi)置函數(shù)的簡(jiǎn)單示例是 seq(),mean(),max(),sum(x) 和 paste(...) 等。它們由用戶編寫的程序直接調(diào)用。 您可以參考最廣泛使用的 R 函數(shù)。

# 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))

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526

用戶定義的函數(shù)

我們可以在 R 語(yǔ)言中創(chuàng)建用戶定義的函數(shù)。它們特定于用戶想要的,一旦創(chuàng)建,它們就可以像內(nèi)置函數(shù)一樣使用。 下面是一個(gè)創(chuàng)建和使用函數(shù)的例子。

# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
  for(i in 1:a) {
   b <- i^2
   print(b)
  }
}	

調(diào)用函數(shù)

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

調(diào)用沒有參數(shù)的函數(shù)

# 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()

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

使用參數(shù)值調(diào)用函數(shù)(按位置和名稱)

函數(shù)調(diào)用的參數(shù)可以按照函數(shù)中定義的順序提供,也可以以不同的順序提供,但分配給參數(shù)的名稱。

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

[1] 26
[1] 58

使用默認(rèn)參數(shù)調(diào)用函數(shù)

我們可以在函數(shù)定義中定義參數(shù)的值,并調(diào)用函數(shù)而不提供任何參數(shù)以獲取默認(rèn)結(jié)果。 但是我們也可以通過提供參數(shù)的新值來(lái)獲得非默認(rèn)結(jié)果來(lái)調(diào)用這樣的函數(shù)。

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

[1] 18
[1] 45

功能的延遲計(jì)算

對(duì)函數(shù)的參數(shù)進(jìn)行延遲評(píng)估,這意味著它們只有在函數(shù)體需要時(shí)才進(jìn)行評(píng)估。

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default

到此這篇關(guān)于R語(yǔ)言函數(shù)詳解及實(shí)例用法的文章就介紹到這了,更多相關(guān)R語(yǔ)言函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論