What is a function and how to create a function using R?

750    Asked by NikitaGavde in Data Science , Asked on Nov 5, 2019
Answered by Nitin Solanki

Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.

On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again. Let us provide an example of function with no inputs

# Simple function, no inputs!

hello <- function(){

    print('hello!')

}

hello()

[1] "hello!"

Let us provide one more example

helloyou <- function(name){

    print(paste('hello ',name))

}

helloyou('Sammy')

[1] "hello Sammy"



Your Answer

Interviews

Parent Categories