Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language function


May 12, 2021 R language tutorial


Table of contents


A function is a set of statements that are grouped together to perform a specific task. T he R language has a large number of built-in functions that users can create their own.

In the R language, the function is an object, so the R language interpreter is able to pass control to the function and the parameters required for the function to complete the action.

The function performs its tasks in turn and returns control to the interpreter and any results that can be stored in other objects.

The function definition

Use keyword functions to create functions in the R language. T he basic syntax of the function definition in the R language is as follows

function_name <- function(arg_1, arg_2, ...) {
   Function body 
}

The function component

Different parts of the function -

  • Function Name - This is the actual name of the function. I t is stored in the R environment as an object with this name.

  • Argument - The argument is a placeholder. W hen a function is called, you pass a value to the argument. T he parameters are optional; T hat is, a function may not contain arguments. A rguments can also have default values.

  • Function body - A body of statements that contains the functions that define a function.

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

The R language has many built-in functions that can be called directly in a program without having to define them first. W e can also create and use our own functions, called user-defined functions.

Built-in features

Simple examples of built-in functions are seq(), mean(), max(), sum(x), and paste(...). a nd so on. T hey are called directly by a user-written program. You can refer to the most widely used R function.

# 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 frm 41 to 68.
print(sum(41:68))

When we execute the code above, it produces the following results -

[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526

A user-defined function

We can create user-defined functions in the R language. T hey are specific to what the user wants, and once created, they can be used like built-in functions. H ere is an example of creating and using functions.

# 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

# 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)

When we execute the code above, it produces the following results -

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

Calls functions that do not have parameters

# 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()

When we execute the code above, it produces the following results -

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

Call functions by location and name using parameter values

The arguments called by the function can be provided in the order defined in the function, or in different order, but assigned to the name of the argument.

# 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)

When we execute the code above, it produces the following results -

[1] 26
[1] 58

The function is called with the default parameters

We can define the value of the argument in the function definition and call the function without providing any arguments to get the default result. B ut we can also call such a function by providing a new value for the argument to get a non-default result.

# 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)

When we execute the code above, it produces the following results -

[1] 18
[1] 45

The delay calculation of the feature

Delayed evaluation of function parameters means that they are evaluated only when the function body needs them.

# 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)

When we execute the code above, it produces the following results -

[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default