User defined functions in R

User defined functions in R

User defined functions in R:

We can create user defined function by using function() .

We create a function object as:

function_name<- function(argument) {

    #Function Body

}

The different parts of a function are –

Function Name – This is the actual name of the function . It is stored as an object in R.

Arguments – It is use as a placeholder. When a function is invoked , you pass a value to the argument. Arguments can be optional .

Function Body – The function body contains a collection of statements that defines what the function does.

Return Value – The return value of a function is the last expression in the function body to be evaluated.

We create a function name “f” which is used to calculate square of a number.

f <- function(x,y){

        x^2

}

f stores structure of function .

We can called function by passing parameters in to it.

We want to find square of 2. We pass the value as argument to the function.

f(2)

Similarly , we find square of number -3 .

f(-3)

We create a new function “f” which print two numbers .

add <- function(x,y){

        x+y

}

We call function add with two parameters .

add(4,5)

We create a function to select observation on base of condition.

Here , we select only those observation where vector x values greater than n.

above <- function(x, n){

        sub <- x > n

        x[sub]

}

a <- c(2,3,4,6,11,22,4,56,6)

We call function on vector “a” . We want to show values in “a” greater than 4.

above(a,4)

We are providing n=5 value in function argument .

x <- 1:10

above <- function(x, n= 5){  

        sub <- x > n

        x[sub]

}

We invoked above function as :

above(x)

It shows values greater than 5.

We create function to find means of each column of y . We use for loop to find means of all columns of  y.

colmean <- function(y){

        nc <- ncol(y)

        means <- as.numeric()

        for(i in 1: nc){

                means[i] <- mean(y[,i])

        }

        means

}

We want to find means of each column of airquality dataset.

colmean(airquality)

We create a character vector “names1” .

names1 <- c(“Dave”, “John”, “Ann”, “Roger”, “Bill”, “Kathy”)

Here , we are using break statement to stop loop when name equal to “Roger” . We use for loop to go through all elements of x . We use if to check if name equals to “Roger” . If the condition is TRUE then the loop is break .

f.names <- function(x) {

        for(name in x){

                if(name==”Roger”)

                        break

                print(name)

        }

}

We call function by passing argument as names1 . So  , it shows three names.

f.names(names1)

We create a simple function to create two new variables z1 and z2 . We use list() to store z1 and z2 as result1 and result2 lists.

f4 <- function(x=3, y=2) {

        z1 <- x + y

        z2 <- x + 2*y

        list(result1=z1, result2=z2)

}

We called function in different forms as :

f4()

Here , we called function by default parameters . So, here x=3 and y=2 .

f4(1,  )$result1

We called function with x=1 and y=2 as default parameter . We only want to see result1 value.

f4(x=1)$result1

We called function with x=1 and y=2 as default parameter . We only want to see result1 value.

f4(, 1)$result1

We called function with x=3 as default parameter and y=1 . We only want to see result1 value.

f4(y=1)$result1

We called function with x=3 as default parameter and y=1 . We only want to see result1 value.

f4(y = 1, x = 2)$result2

We called function with x=2 and y=1 . We only want to see result2 value.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top