溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-03-29 09:21:38 來源:億速云 閱讀:238 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)R語言函數(shù)詳解及實例用法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

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

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

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

函數(shù)定義

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

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

函數(shù)組件

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

  • 函數(shù)名稱 -這是函數(shù)的實際名稱。 它作為具有此名稱的對象存儲在 R 環(huán)境中。

  • 參數(shù) -參數(shù)是一個占位符。 當(dāng)函數(shù)被調(diào)用時,你傳遞一個值到參數(shù)。 參數(shù)是可選的; 也就是說,一個函數(shù)可能不包含參數(shù)。 參數(shù)也可以有默認(rèn)值。

  • 函數(shù)體 -函數(shù)體包含定義函數(shù)的功能的語句集合。

  • 返回值 -函數(shù)的返回值是要評估的函數(shù)體中的最后一個表達式。

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

內(nèi)置功能

內(nèi)置函數(shù)的簡單示例是 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 語言中創(chuàng)建用戶定義的函數(shù)。它們特定于用戶想要的,一旦創(chuàng)建,它們就可以像內(nèi)置函數(shù)一樣使用。 下面是一個創(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ù)的新值來獲得非默認(rèn)結(jié)果來調(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

功能的延遲計算

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

# 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語言函數(shù)詳解及實例用法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI