函數(shù)用于在邏輯上將我們的代碼分解成更簡(jiǎn)單的部分,這些部分易于維護(hù)和理解。如果代碼中需要多次重復(fù)某個(gè)操作,將其寫成函數(shù)會(huì)使代碼更簡(jiǎn)潔。
1. 語(yǔ)法
在R中創(chuàng)建自己的函數(shù)非常簡(jiǎn)單,其語(yǔ)法為:
func_name <- function (argument) {
statement
}
可以看到:
R中用保留字
function來(lái)聲明一個(gè)函數(shù)。花括號(hào)內(nèi)的語(yǔ)句構(gòu)成函數(shù)的主體。 如果主體僅包含一個(gè)表達(dá)式,則花括號(hào)可以不要。
最后,通過(guò)將函數(shù)對(duì)象分配給變量
func_name來(lái)為其命名。
2. 例子
來(lái)看個(gè)例子:
pow <- function(x, y) {
# function to print x raised to the power y
result <- x^y
print(paste(x, "raised to the power", y, "is", result))
}
在這里,我們創(chuàng)建了一個(gè)名為pow()的函數(shù)。
它有兩個(gè)參數(shù),第二個(gè)參數(shù)是第一個(gè)參數(shù)的冪,然后以適當(dāng)?shù)母袷酱蛴〗Y(jié)果。
我們使用了內(nèi)置函數(shù)paste()來(lái)連接字符串。
3. 調(diào)用函數(shù)
我們可以如下調(diào)用上述函數(shù):
> pow(8, 2)
[1] "8 raised to the power 2 is 64"
> pow(2, 8)
[1] "2 raised to the power 8 is 256"
這里,函數(shù)聲明中使用的參數(shù)(x和y)稱為形式參數(shù),而調(diào)用函數(shù)時(shí)使用的參數(shù)稱為實(shí)際參數(shù)。
4. 命名參數(shù)
在上述函數(shù)調(diào)用中,形式參數(shù)與實(shí)際參數(shù)的參數(shù)匹配是按位置順序進(jìn)行。
這意味著,在調(diào)用pow(8, 2)中,形式參數(shù)x和y分別分配了8和2。
我們也可以使用命名參數(shù)調(diào)用該函數(shù)。
以這種方式調(diào)用函數(shù)時(shí),實(shí)際參數(shù)的順序就無(wú)關(guān)緊要了。 例如,下面給出的所有函數(shù)調(diào)用都是等效的:
> pow(8, 2)
[1] "8 raised to the power 2 is 64"
> pow(x = 8, y = 2)
[1] "8 raised to the power 2 is 64"
> pow(y = 2, x = 8)
[1] "8 raised to the power 2 is 64"
此外,我們可以在單個(gè)調(diào)用中同時(shí)使用命名和未命名的參數(shù)。
在這種情況下,將首先匹配所有已命名的參數(shù),然后按位置順序匹配其余未命名的參數(shù)。
> pow(x=8, 2)
[1] "8 raised to the power 2 is 64"
> pow(2, x=8)
[1] "8 raised to the power 2 is 64"
在上述所有例子中,x的取值為8,y的取值為2。
5. 參數(shù)的默認(rèn)值
函數(shù)中的參數(shù)也可以事先分配默認(rèn)值。
這可以通過(guò)在函數(shù)聲明中將形式參數(shù)賦予一個(gè)值來(lái)實(shí)現(xiàn)。
下面是為上述函數(shù)提供了一個(gè)默認(rèn)的y值。
pow <- function(x, y = 2) {
# function to print x raised to the power y
result <- x^y
print(paste(x,"raised to the power", y, "is", result))
}
對(duì)使用默認(rèn)值的參數(shù),在調(diào)用時(shí)可以不提供。
> pow(3)
[1] "3 raised to the power 2 is 9"
> pow(3, 1)
[1] "3 raised to the power 1 is 3"
此處,y是可選的,當(dāng)未提供y時(shí)將默認(rèn)取值為2。
6. 函數(shù)的返回值
很多時(shí)候,我們將需要使用函數(shù)進(jìn)行一些處理并返回結(jié)果。這是通過(guò)R中的return()函數(shù)來(lái)完成的。
其語(yǔ)法為:
return(expression)
函數(shù)的返回值可以是任何有效對(duì)象。
來(lái)看個(gè)例子:
check <- function(x) {
if (x > 0) {
result <- "Positive"
}
else if (x < 0) {
result <- "Negative"
}
else {
result <- "Zero"
}
return(result)
}
下面是一些運(yùn)行示例:
> check(1)
[1] "Positive"
> check(-10)
[1] "Negative"
> check(0)
[1] "Zero"
如果函數(shù)沒有顯式返回,則最后一個(gè)計(jì)算表達(dá)式的值將自動(dòng)返回。
例如,下面的函數(shù)等價(jià)于上面的函數(shù):
check <- function(x) {
if (x > 0) {
result <- "Positive"
}
else if (x < 0) {
result <- "Negative"
}
else {
result <- "Zero"
}
result
}
> check(2)
[1] "Positive"
> check(-3)
[1] "Negative"
我們通常使用顯式return()函數(shù)立即從函數(shù)返回值。
如果return()不是該函數(shù)的最后一條語(yǔ)句,它將提前結(jié)束該函數(shù),程序的控制權(quán)將回到調(diào)用它的位置。
來(lái)看個(gè)例子:
check <- function(x) {
if (x>0) {
return("Positive")
}
else if (x<0) {
return("Negative")
}
else {
return("Zero")
}
}
我們來(lái)試著調(diào)用上述函數(shù):
> check(-5)
[1] "Negative"
在上面的示例中,如果x<0,該函數(shù)將立即返回Negative,將不再執(zhí)行函數(shù)的其余部分。
return()函數(shù)只能返回一個(gè)對(duì)象。如果我們想返回多個(gè)值,我們可以使用一個(gè)列表(或其他對(duì)象)作為返回值。
下面是一個(gè)例子:
multi_return <- function() {
my_list <- list("color" = "red", "size" = 20, "shape" = "round")
return(my_list)
}
在這里,我們創(chuàng)建一個(gè)包含多個(gè)元素的列表my_list并返回這個(gè)列表。試一下這個(gè)函數(shù):
> a <- multi_return()
> a$color
[1] "red"
> a$size
[1] 20
> a$shape
[1] "round"
今天關(guān)于函數(shù)的內(nèi)容就介紹到這,希望對(duì)大家有點(diǎn)幫助。
感謝您的閱讀!想了解更多有關(guān)技巧,請(qǐng)關(guān)注我的微信公眾號(hào)“R語(yǔ)言和Python學(xué)堂”,我將定期更新相關(guān)文章。
