Function in R



A function is a set of codes which executes at a time and used to perform some actions




Function Syntax

Function Name
Function Body
Return Value
Input Values

Name                               Input          Body               ReturnValue
demo.function <- function(a)       {    b <- a^2           return(b)    }
Function Code snippet
#Function without input/output
Definition
emptyFunction <- function()
{
 print("hello")
}
Calling
emptyFunction()

#Function with input
demo.function <- function(a)
 {
   for(i in 1:a)
   {
      b <- i^2
      print(b)
 }
}       

demo.function(5)

# Function with return
demoReturn <- function()
{
        x <- 1
 z <- 2
 return(x)
 }
 sampleData <- demoReturn()
 sampleData
 # Function with input/output
demo.function <- function(a)
 {
      b <- a^2
     return(b)
}

sampleData <- demo.function(5)
sampleData

Recursive functions

recursive.factorial <- function(x)
{
   if (x == 0) { return (1)}
   else
   {return (x * recursive.factorial(x-1))}
}
recursive.factorial(5)
Function in R Reviewed by Rupesh on 11:47 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author 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.

Contact Form

Name

Email *

Message *

Powered by Blogger.