R Functions

A function is a self-contained module that accomplishes a specific task. Functions take input arguments and return results by executing the functions’ expressions. Functions allow us to automate repetitive tasks by making our code more readable.

How to call a function in R?

To call or invoke a function, type the function name, followed by the round parentheses. The value to be evaluated goes inside the parentheses. For example, if you want to sum numbers from 1 to 1000, you can use the built-in sum() function. To call this function, run the following code:

sum(1:1000)

Output

[1] 500500

Here, we have given the function’s name, followed by the parentheses and the arguments inside of it.

Types of functions in R

R Functions
Types of functions in R

There are 2 types of functions in R:

  1. Built-in functions: R comes with a set of pre-created functions that we can use. For example, substr(), sqrt(), mean() etc.
  2. User-defined functions: As the name suggests, User-defined functions are written by the users.

Built-in functions in R

Can be categorized into:

Numeric Functions

Functions used on numeric values are called numeric functions. Common numeric functions are: abs(), sqrt(), floor(), ceiling(), log(), exp() etc.

Examples

abs(-5) #returns the absolute value
[1] 5
sqrt(64) #returns the square root of the number
[1] 8
floor(3.14159) #returns the integer below the passed in number
[1] 3
ceiling(3.14159) #returns the integer above the passed in number
[1] 4
log(10) #returns the logarithm of the number
[1] 2.302585
exp(10) #retuns the exponent of the number
[1] 22026.47

Character Functions

Functions used to manipulate the characters or strings are Character functions. Common character functions are: toupper(), tolower(), grepl(), strsplit(), paste() etc.

Examples

toupper("stop yelling!") #converts into uppercase
[1] "STOP YELLING!"
tolower("WELCOME") #converts into lowercase
[1] "welcome"
grepl("e","Frisbee") #finds the character “e” in the string “Frisbee”
[1] TRUE
strsplit("go-to-go","-") #splits the strings on the basis of delimiter “-”
[1] "go" "to" "go"
paste("Ryan", "Broda", sep = "_") #concatenate the strings with the separator “_”
[1] "Ryan_Broda"

Statistical Functions

Common Statistical functions are: mean(), median(), max(), min().

Examples

x <- c(4,55,78,99,98,67)
mean(x) #returns the average of the numbers
[1] 66.83333|
median(x) #returns the median of the numbers
[1] 72.5
max(x) #returns the maximum of the numbers
[1] 99
min(x) #returns the minimum of the numbers
[1] 4

Other Functions

Some of the other widely used functions include: c(), rep(), seq() etc.

Examples

c(4,55,78,99,98,67) #returns a vector of numbers
[1] 4 55 78 99 98 67
rep("Hello", 5) #repeats a value(or values) a specific number of times
[1] "Hello" "Hello" "Hello" "Hello" "Hello"
seq(from = 1, to = 10) #starts a sequence of number from one value to another
[1] 1 2 3 4 5 6 7 8 9 10

User-defined functions

Let’s create a simple function to add one to the passed number.

add_one = function(X){
X+1
}
add_one(500)

Output

[1] 501

In the above example, add_one is the name of the function. X is the input argument. X+1 denotes the operation to increment the input argument by one. To invoke the function, we passed in the function name followed by the parentheses and the arguments inside it.

Leave a Comment