Beginners Guide to R – R Functions

Hits: 9

R Functions

In simple terms, a function is a block of statements that can be used repeatedly in a program. R provides many built-in functions and allows programmers to define their own functions.

Syntax

Here’s the syntax of a function in R:

r function syntax

Create a Function

To define a function in R, use the function command and assign the results to a function name.

# Create a function 'myfunc'
myfunc <- function() {
  print('Hello, World!')
}

If you have only one statement to execute, you can skip curly braces.

# Write a function in one line
myfunc <- function() print('Hello, World!')

Call a Function

You can call (run) the function by adding parentheses after the function’s name.

myfunc <- function() {
  print('Hello, World!')
}

myfunc()
[1] "Hello, World!"

Pass Arguments

You can send information to a function through arguments. Arguments are declared after the function keyword in parentheses.

You can send as many arguments as you like, just separate them by a comma ,.

sum <- function(x, y) {
  x + y
}

sum(2, 3)
[1] 5

Named Arguments

If you pass arguments to a function by name, you can put those arguments in any order.

pow <- function(x, y) {
  x ^ y
}

# using argument names
pow(x=2, y=3)
[1] 8

# changing the order
pow(y=3, x=2)
[1] 8

Default Argument Value

You can assign a default value to an argument. So, when you call the function without argument, it uses the default value.

# Set default value ‘3’ to second argument
pow <- function(x, y=3) {
  x ^ y
}

# function will use default y value
pow(2)
[1] 8

# specifying a different y value
pow(2, 4)
[1] 16

Return a Value

To return a value from a function, simply use a return() function.

sum <- function(x, y) {
  return(x + y)
}

sum(2, 3)
[1] 5

If you do not include any return() function, it automatically returns the last expression.

sum <- function(x, y) {
  x + y
}

sum(2, 3)
[1] 5

Return Multiple Values

You can return multiple values by saving the results in a vector (or a list) and returning it.

math <- function(x, y) {
  add <- x + y
  sub <- x - y
  mul <- x * y
  div <- x / y
  c(addition = add, subtraction = sub,
    multiplication = mul, division = div)
}

math(6, 3)
      addition    subtraction multiplication       division 
             9              3             18              2

Lazy Evaluation

R functions perform lazy evaluation that dramatically extends the expressive power of functions. It is the technique of not evaluating arguments unless and until they are needed in the function.

myfunc <- function(x, y) {
  if(!x){
    return(y)
  }
  else{
    return(x)
  }
}

# y is not evaluated so not including it causes no harm
myfunc(6)
[1] 6

# y is evaluated so not including it raises error
myfunc(0)
Error in myfunc(0) : argument "y" is missing, with no default

Variable Length Argument

In R, it is often convenient to accept a variable number of arguments passed to the function. To do this, you specify an ellipsis (...) in the arguments when defining a function.

It accepts variable number of arguments, in the sense that you do not know beforehand how many arguments can be passed to your function by the user.

For example, below function prints the first argument and then passes all the other arguments to the summary() function.

myfunc <- function(x,...) {print(x); summary(...)}
v <- 1:10

myfunc("Summary of v:", v)
[1] "Summary of v:"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

You can also directly refer to the arguments within the argument list (...) through the variables ..1, ..2, to ..9.

For example, ..1 refers to the first argument, ..2 refers to the second, and so on.

myfunc <- function(...) {cat(..1, ..2)}

myfunc("Hello", "World!")
Hello World!

It is also possible to read the arguments from the argument list by converting the object (...) to a list within the function body.

For example, below function simply sums all its arguments:

addAll <- function(x,...) {
  args <- list(...)
  for (a in args) x <- x + a
  x
}

addAll(1,2)
[1] 3

addAll(1,2,3,4,5)
[1] 15

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.