0% found this document useful (0 votes)
40 views

Functions in R, Math

The document discusses functions in R, including built-in and user-defined functions. Functions allow users to organize code into reusable blocks to perform specific tasks and avoid duplicating code. R has many built-in mathematical, summary, string, data frame, and vector functions, as well as the ability for users to create their own custom functions using the function keyword.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Functions in R, Math

The document discusses functions in R, including built-in and user-defined functions. Functions allow users to organize code into reusable blocks to perform specific tasks and avoid duplicating code. R has many built-in mathematical, summary, string, data frame, and vector functions, as well as the ability for users to create their own custom functions using the function keyword.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Functions in R

• A set of statements which are organized together to perform a specific task is known as a
function.
• R provides a series of in-built functions, and it allows the user to create their own
functions.
• Functions are used to perform tasks in the modular approach.
• Functions are used to avoid repeating the same task and to reduce complexity.
• To understand and maintain our code, we logically break it into smaller parts using the
function.
A function should be

• Written to carry out a specified task.


• May or may not have arguments
• Contain a body in which our code is written.
• May or may not return one or more output values.
• "An R function is created by using the keyword function.”

1.func_name <- function(arg_1, arg_2, ...) {  
2.   Function body   
3.}  
Components of Functions

• There are four components of function, which are as follows:


• Function Name
• The function name is the actual name of the function. In R, the function is stored as an object
with its name.
• Arguments
• In function, arguments are optional means a function may or may not contain arguments, and
these arguments can have default values also. We pass a value to the argument when a function
is invoked.
• Function Body
• The function body contains a set of statements which defines what the function does.
• Return value
• It is the last expression in the function body which is to be evaluated.
Function Types

• Similar to the other languages, R also has two types of function, i.e. Built-in
Function and User-defined Function.
• In R, there are lots of built-in functions which we can directly call in the program without
defining them. R also allows us to create our own functions.
•       
Built-in function

• The functions which are already created or defined in the programming framework are
known as built-in functions.
• User doesn't need to create these types of functions, and these functions are built into an
application. End-users can access these functions by simply calling it.
• R have different types of built-in functions such as seq(), mean(), max(), and sum(x) etc.
# Creating sequence of numbers from 32 to 46.  
print(seq(32,46))  
  
# Finding the mean of numbers from 22 to 80.  
print(mean(22:80))  
  
# Finding the sum of numbers from 41 to 70.  
print(sum(41:70))  
User-defined function

• R allows us to create our own function in our program. A user defines a user-define
function to fulfill the requirement of user. Once these functions are created, we can use
these functions like in-built function.

# Creating a function without an argument.


new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}

new.function()
R has a rich set of functions that can be used to perform almost every task for the
user.

Mathematical Functions

Summary Functions

String Functions

data frame functions

vector functions
Mathematical Functions

• R provides the various mathematical functions to perform the mathematical calculation.


• These mathematical functions are very helpful to find absolute value, square value and
much more calculations.
S. No Function Description Example
1. abs(x) It returns the absolute value of input x. x<- -4 print(abs(x)) Output[1] 4
2. sqrt(x) It returns the square root of input x. x<- 4 print(sqrt(x)) Output[1] 2
3. ceiling(x) It returns the smallest integer which is x<- 4.5 print(ceiling(x)) Output[1] 5
larger than or equal to x.
4. floor(x) It returns the largest integer, which is x<- 2.5 print(floor(x)) Output[1] 2
smaller than or equal to x.
5. trunc(x) It returns the truncate value of input x. x<- c(1.2,2.5,8.1) print(trunc(x)) Output[1] 1 2 8
6. round(x, It returns round value of input x. x<- -4 print(abs(x)) Output4
digits=n)
7. cos(x), sin(x), It returns cos(x), sin(x) value of input x. x<- 4 print(cos(x)) print(sin(x)) print(tan(x))
tan(x) Output[1] -06536436 [2] -0.7568025 [3] 1.157821

8. log(x) It returns natural logarithm of input x. x<- 4 print(log(x)) Output[1] 1.386294

9. log10(x) It returns common logarithm of input x. x<- 4 print(log10(x)) Output[1] 0.60206

10. exp(x) It returns exponent. x<- 4 print(exp(x)) Output[1] 54.59815


ceiling() and floor()

The ceiling() function rounds a number upwards to its nearest


integer
the floor() function rounds a number downwards to its nearest
integer, and returns the result.
ceiling(1.4)

floor(1.4)
Log()
Takes the logarithm of x with base y; if base is not specified,
returns the natural logarithm.
 you can take the logarithm of the numbers from 1 to 3
log(1:3)
• Whenever you use one of these functions, R calculates the
natural logarithm if you don’t specify any base.
• You calculate the logarithm of these numbers with base 6 like
this.
• log(1:3,base=6)
trunc(x) and round(x, digits=n)

trunc(x) :It returns the truncate value of input x.


x<- c(1.2,2.6,8.1)
print(trunc(x))

round(x, digits=n): It returns round value of input x.

You might also like