跳轉到內容

統計分析:使用 R/R/函式入門

來自華夏公益教科書,開放的書籍,面向開放的世界
除了數字之外,R 中可能最有用的命名物件是函式。您在 R 中執行的幾乎所有有用操作都是使用函式完成的,並且許多函式預設情況下在 R 中可用。您可以透過鍵入函式名稱後跟一對圓括號來使用(或“呼叫”)函式。例如,啟動文字提到了以下函式,如果您想在已釋出的作品中引用 R,您可能會發現它很有用。
輸入
citation()
結果
> citation() 在出版物中引用 R 時,請使用:R Development Core Team (2008)。R:一種用於統計計算的語言和環境。R Foundation for Statistical Computing,維也納,奧地利。ISBN 3-900051-07-0,URL http://www.R-project.org。LaTeX 使用者的 BibTeX 條目是 @Manual{, url = {http://www.R-project.org}, title = {R: A Language and Environment for Statistical Computing}, author = {{R Development Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2008}, note = {{ISBN} 3-900051-07-0}, } 我們投入了大量時間和精力來建立 R,請在使用它進行資料分析時引用它。另請參見“citation("pkgname")”以引用 R 包。
許多 R 函式可以根據您提供給它們的引數生成不同的結果。引數放在圓括號內,用逗號分隔。許多函式有一個或多個可選引數:也就是說,您可以選擇是否提供它們。citation() 函式就是一個例子。它可以接受一個可選引數,該引數給出R 附加包的名稱。如果您不提供可選引數,通常會有一個假設的預設值(在citation() 的情況下,此預設值為"base",即提供基本包的引用:提供 R 語言大多數基礎的包)。

函式的大多數引數都是命名的。例如,citation 函式的第一個引數名為package。為了提供額外的清晰度,在使用函式時,您可以提供長格式的name=value 引數。因此

citation("base")

citation(package="base")
相同。如果函式可以接受多個引數,使用長格式還允許您更改引數的順序,如下面的示例程式碼所示。
輸入
citation("base")      #Does the same as citation(), because the default for the first argument is "base"
                      #Note: quotation marks are needed in this particular case (see discussion below)
citation("datasets")  #Find the citation for another package (in this case, the result is very similar)
sqrt(25)              #A different function: "sqrt" takes a single argument, returning its square root.
sqrt(25-9)            #An argument can contain arithmetic and so forth
sqrt(25-9)+100        #The result of a function can be used as part of a further analysis
max(-10, 0.2, 4.5)    #This function returns the maximum value of all its arguments
sqrt(2 * max(-10, 0.2, 4.5))             #You can use results of functions as arguments to other functions
x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100  #... and you can store the results of any of these calculations
x
log(100)              #This function returns the logarithm of its first argument
log(2.718282)         #By default this is the natural logarithm (base "e")
log(100, base=10)     #But you can change the base of the logarithm using the "base" argument
log(100, 10)          #This does the same, because "base" is the second argument of the log function
log(base=10, 100)     #To have the base as the first argument, you have to use the form name=value
結果
> citation("base") #與 citation() 相同,因為第一個引數的預設值為 "base" 在出版物中引用 R 時,請使用:R Development Core Team (2008)。R:一種用於統計計算的語言和環境。R Foundation for Statistical Computing,維也納,奧地利。ISBN 3-900051-07-0,URL http://www.R-project.org。LaTeX 使用者的 BibTeX 條目是 @Manual{, title = {R: A Language and Environment for Statistical Computing}, author = {{R Development Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2008}, note = {{ISBN} 3-900051-07-0}, url = {http://www.R-project.org}, } 我們投入了大量時間和精力來建立 R,請在使用它進行資料分析時引用它。另請參見“citation("pkgname")”以引用 R 包。 > #注意:在這種特定情況下需要引號(見下文討論) > citation("datasets") #查詢另一個包的引用(在本例中,結果非常相似) 'datasets' 包是 R 的一部分。在出版物中引用 R 時,請使用:R Development Core Team (2008)。R:一種用於統計計算的語言和環境。R Foundation for Statistical Computing,維也納,奧地利。ISBN 3-900051-07-0,URL http://www.R-project.org。LaTeX 使用者的 BibTeX 條目是 @Manual{, title = {R: A Language and Environment for Statistical Computing}, author = {{R Development Core Team}}, organization = {R Foundation for Statistical Computing}, address = {Vienna, Austria}, year = {2008}, note = {{ISBN} 3-900051-07-0}, url = {http://www.R-project.org}, } 我們投入了大量時間和精力來建立 R,請在使用它進行資料分析時引用它。另請參見“citation("pkgname")”以引用 R 包。 > sqrt(25) #一個不同的函式:"sqrt" 接受一個引數,返回其平方根。 [1] 5 > sqrt(25-9) #引數可以包含算術等 [1] 4 > sqrt(25-9)+100 #函式的結果可以用作進一步分析的一部分 [1] 104 > max(-10, 0.2, 4.5) #此函式返回所有引數的最大值 [1] 4.5 > sqrt(2 * max(-10, 0.2, 4.5)) #您可以將函式的結果用作其他函式的引數 [1] 3 > x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100 #... 您可以儲存所有這些計算的結果 > x [1] 103 > log(100) #此函式返回其第一個引數的對數 [1] 4.60517 > log(2.718282) #預設情況下,這是自然對數(以“e”為底) [1] 1 > log(100, base=10) #但您可以使用 "base" 引數更改對數的底數 [1] 2 > log(100, 10) #這與上面相同,因為 "base" 是 log 函式的第二個引數 [1] 2 > log(base=10, 100) #要將底數作為第一個引數,您必須使用 name=value 形式 [1] 2
請注意,在鍵入普通文字(如包的名稱)時,需要用引號[1]將其包圍,以避免與物件的名稱混淆。換句話說,在 R 中
citation

指的是一個函式,而

"citation"

是一個文字“字串”。這在提供繪圖示題等時非常有用。

您可能會發現,瞭解 R 的最棘手方面之一是知道在特定情況下使用哪個函式。幸運的是,R 不僅提供了所有函式的文件,還提供了搜尋文件的方法,以及其他獲取幫助的方法。


  1. 您可以使用單引號(')或雙引號(")來分隔文字字串,只要開始和結束引號匹配即可
華夏公益教科書