You are currently viewing FUN in R – Usage with Example

FUN translates to function in R, in most cases it is used when you wanted to apply a function over a List or Vector. You will find this in many R method syntaxes. For example, you can find this on apply(), lapply(), vapply(), sapply() e.t.c

In this article, I will explain some examples of where FUN is used in R Programming and how to create a function and use it with vectors and lists.

1. FUN in R

Let’s see some function syntaxes that have FUN in their method syntaxes. Let’s quickly see what each function does.

  • lapply – Returns a list of the same length as X. It can take vector as input and always return a list
  • sapply – User-friendly version and wrapper of lapply. This by default return a vector, matrix or, an array.
  • vapply – Same as sapply, but has a pre-specified type of return value.

lapply(X, FUN, ...)

sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

Below are details about some arguments that I will be using here.

  • X – A vector or list where you wanted to apply a function.
  • FUN – the function to be applied to each element of X
  • ... – Optional. Arguments to FUN (Function)

2. Apply FUN on List or Vector in R

Let’s use the FUN param of R lapply() function to convert the elements of the vector to lowercase. Since lapply() return the list, our result will be in a list with lowercase strings. You can use unlist() to convert list to vector in R.


# Using lapply() with FUN
vec <- c('JAVA','R','PYTHON','PHP')
print(vec)

# apply function to vector
res <- lapply(vec, FUN=tolower)
res

Yields below output.

FUN in r

3. Use FUN=fun() in R (Custom function)

Now let’s create a custom function in R and call it from lapply() function by passing the function name to the FUN argument.

Here, the getLang() function is called for each element of the vector and the function getLang() split the string by delimiter ‘_’ and returns the first part of the string.


# custom function
# function to split and get the first part
getLang <- function(str) { 
  return (unlist(strsplit(str,'_'))[1])
}

# create vector
vec <- c('JAVA_LANG','R_LANG','PYTHON_LANG','PHP_LANG')
vec

# apply function
res <- lapply(vec, FUN=getLang)
res

Yields below output.

4. FUN with Arguments in R

Finally, let’s call the function with arguments. In the previous example, I hardcoded the ‘_’ delimiter getLang() that I used to split the string, In the below example I am taking delimiter as an argument to the function getLang() so, I can change the delimiter in runtime with a function call.


# function to split and get the first part
# Pass as an argument if you wanted to use different delimiter to split
getLang <- function(str="", delimiter=',') { 
  return (unlist(strsplit(str,delimiter))[1])
}

# create vector
vec <- c('JAVA_LANG','R_LANG','PYTHON_LANG','PHP_LANG')
vec

# apply function
res <- lapply(vec, FUN=getLang,delimiter='_')
res

Yields the same output as above.

Conclusion

In this article, you have learned what is FUN in R and how to use it. FUN translates to function in R, in most cases it is used when you wanted to apply a function over a List or Vector. You will find this in many R method syntaxes. For example, you can find this on lapply(), vapply(), sapply() e.t.c

You can find the complete example at GitHub R Examples

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium