There are two different types of functions in R, user-defined functions and built-in functions.
A user-defined function provides customization and flexibility to users to write their functions to perform computations. It has a general notation shown as follows:
newFunc <- function(x){define function} > int<-seq(1:20) > int [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > myfunc<-function(x){x*x} > myfunc(int) [1] 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
In the preceding script, we are creating a small sequence of numbers from 1 to 20 and a user-defined function to calculate the square of each integer. Using the new function, we can calculate the square of any number. So the user can define and create his or her own custom function.
Built-in functions, such as mean, median, standard deviation, and so on, provide the user the ability to compute basic statistics using R. There are many built-in functions; the following table displays a few important built-in functions:
Function |
Description |
|
Absolute value |
|
Square root |
|
Rounding up the number |
|
Rounding down the number |
|
|
|
|
|
|
|
Also |
|
Natural logarithm |
|
Common logarithm |
|
|
Table 2: Some built-in functions
18.116.36.174