This tutorial will show us how to use the funs()
in R. The funs in R will help you create a list of function calls. So if you want to know more about this, read it entirety.
Funs in R
What does the funs() do in R?
In R programming language, this function is used to create a list of function calls.
The syntax:
If you use the jqr package, we will provide the syntax for this package below:
funs(input, func, act)
Parameters:
- input: The data input.
- func: Function is defined without ‘def’.
- act: The action that you will work with the data.
Besides, you can use the dplyr package to create a list of function calls. Follow the syntax of this package.
funs(input, .args = list())
Parameters
- input: The list of functions, ex: their names, function, …
- .args: The list of the name arguments is added to all function calls.
How to use the funs() in R?
Use the funs function with the jqr package
First, if you want to use the funs function, you need to add the jqr package. You can click here if you don’t know how to add the package.
The following is an example of the funs in R. We will use the funs function to calculate addition, subtraction, multiplication, and division of array elements. Look at the code example below.
if (!require('jqr')) { install.packages('jqr') library('jqr') } jq("[10,20,45,93,26]", 'def calculate: . + 100; map(calculate)') "[10,20,45,93,26]" %>% funs('calculate: . - 100', 'map(calculate)') "[10,20,45,93,26]" %>% funs('calculate: . * 100', 'map(calculate)') "[10,20,45,93,26]" %>% funs('calculate: . / 100', 'map(calculate)')
Output
[110,120,145,193,126]
[-90,-80,-55,-7,-74]
[1000,2000,4500,9300,2600]
[0.1,0.2,0.45,0.93,0.26]
Use the funs function with dplyr package
Similar to the jqr package, we also add the dplyr package. Here, we will function calls by using the dplyr package. Check out the code below to better understand.
if (!require('dplyr')) { install.packages('dplyr') library('dplyr') } # Function calls funs("sum", sum(., na.rm = TRUE)) # Function calls funs(s1 = sum, s2 = "sum", s3 = sum(., na.rm = TRUE))
Output
<fun_calls>
$ sum: sum(.)
$ sum: sum(., na.rm = TRUE)
<fun_calls>
$ s1: sum(.)
$ s2: sum(.)
$ s3: sum(., na.rm = TRUE)
Summary
Well, above is all I show you about the syntax and usage of the funs in R. I hope you find a solution that works for you and If you have any questions please leave a comment below. Thanks for reading and good luck to you!
Maybe you are interested:
- Predict Function in R: How To Use Predict() Function In R?
- Merge In R: What is merge() in R and Example
- Sum Function In R: How To Use Sum() In R
Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript