函數(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é)果。
使用關(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í)際名稱。 它作為具有此名稱的對(duì)象存儲(chǔ)在 R 環(huán)境中。
參數(shù) -參數(shù)是一個(gè)占位符。 當(dāng)函數(shù)被調(diào)用時(shí),你傳遞一個(gè)值到參數(shù)。 參數(shù)是可選的; 也就是說(shuō),一個(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)置函數(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 from 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
我們可以在 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) } }
# 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
# 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ù)中定義的順序提供,也可以以不同的順序提供,但分配給參數(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
我們可以在函數(shù)定義中定義參數(shù)的值,并調(diào)用函數(shù)而不提供任何參數(shù)以獲取默認(rèn)結(jié)果。 但是我們也可以通過(guò)提供參數(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
對(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
更多建議: