1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
R Programming
  • Everywhere
  • R Programming
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Scientific Tools Articles
  3. R Programming

What are functions in R ?

  • ScirntificTools.org
  • 0 Comments

In this article, we will explain what functions are in R and give you some real-world examples.

Contents [hideshow]
  1. Basic structure of a function in R
  2. Built-in Functions in R
  3. User-defined Functions
  4. Functions with Default Arguments

In R, a function is a set of statements organized together to perform a specific task. R has a number of built-in functions, and you can also create your own functions, which are known as user-defined functions.

Basic structure of a function in R

R
function_name <- function(arg1, arg2, ...){   # Function body   # Computation goes here   return(value)
}

Built-in Functions in R

R provides a plethora of built-in functions for various tasks. For instance:

  • Arithmetic functions: sum(), mean(), etc.
  • Statistical functions: lm(), t.test(), etc.
  • Graphics-related: plot(), hist(), etc.

Example:

R
# Using the built-in mean function
values <- c(2, 4, 6, 8, 10)
average <- mean(values)
print(average) # Outputs: 6

User-defined Functions

You can create your own functions in R for tasks that you perform frequently or to encapsulate complex logic.

Example:

Let's create a function that calculates the square of a number:

R
# Defining the function
square <- function(x) {  return(x^2)
}

# Using the function
result <- square(5)
print(result) # Outputs: 25

Another example, a function to calculate the factorial of a number:

R
factorial_calc <- function(n) {  if(n <= 1) {    return(1)  } else {    return(n * factorial_calc(n-1))  }
}

print(factorial_calc(5)) # Outputs: 120

Functions with Default Arguments

You can define default values for function arguments. If the function is called without the argument value, it will use the default value.

Example:

R
# A function to raise a number to a power with a default power of 2
power_function <- function(base, exponent = 2) {  return(base^exponent)
}

print(power_function(3))     # Outputs: 9, since it uses the default exponent of 2
print(power_function(3, 3))  # Outputs: 27

In practice, R's capability to handle and create functions is a crucial aspect of the language, especially for data analysis workflows. Functions help to modularize and reuse code, making the overall analysis more organized and manageable.

  • Previous Article Most used Rstudio and R keyboard shortcuts
  • Next Article Functions with multiple arguments in R

Categories

  1. R Programming 15
  2. Linux 0
  3. Reset Filter
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™