R語言函數(shù)基礎知識點總結(jié)
函數(shù)是一組組合在一起以執(zhí)行特定任務的語句。 R 語言具有大量內(nèi)置函數(shù),用戶可以創(chuàng)建自己的函數(shù)。
在R語言中,函數(shù)是一個對象,因此R語言解釋器能夠?qū)⒖刂苽鬟f給函數(shù),以及函數(shù)完成動作所需的參數(shù)。
該函數(shù)依次執(zhí)行其任務并將控制返回到解釋器以及可以存儲在其他對象中的任何結(jié)果。
函數(shù)定義
使用關鍵字函數(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ù)是一個占位符。 當函數(shù)被調(diào)用時,你傳遞一個值到參數(shù)。 參數(shù)是可選的; 也就是說,一個函數(shù)可能不包含參數(shù)。 參數(shù)也可以有默認值。
- 函數(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 from 41 to 68. print(sum(41:68))
當我們執(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)
當我們執(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()
當我們執(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)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[1] 26 [1] 58
使用默認參數(shù)調(diào)用函數(shù)
我們可以在函數(shù)定義中定義參數(shù)的值,并調(diào)用函數(shù)而不提供任何參數(shù)以獲取默認結(jié)果。 但是我們也可以通過提供參數(shù)的新值來獲得非默認結(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)
當我們執(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)
當我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
[1] 36 [1] 6 Error in print(b) : argument "b" is missing, with no default
到此這篇關于R語言函數(shù)基礎知識點總結(jié)的文章就介紹到這了,更多相關R語言函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
R語言數(shù)據(jù)框合并(merge)的幾種方式小結(jié)
這篇文章主要介紹了R語言數(shù)據(jù)框合并(merge)的幾種方式小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
R語言數(shù)據(jù)可視化繪圖Dot plot點圖畫法示例
這篇文章主要為大家介紹了R語言數(shù)據(jù)可視化繪圖Dot plot點圖的畫法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02

