User-defined functions in R Explained

I build, write and explore around AI, data, and technology. Sometimes it’s experiments, sometimes it’s just reflections. All of it helps me learn.
Search for a command to run...

I build, write and explore around AI, data, and technology. Sometimes it’s experiments, sometimes it’s just reflections. All of it helps me learn.
No comments yet. Be the first to comment.
Understanding the Difference: Naive RAG and Re-Ranked RAG Compared in a Real Insurance Scenario

Unlocking the Full Potential of Language Models Without Restrictions

AI is Changing the Game— Essential Terms That Will Transform Your Understanding!

A Comprehensive Setup Guide for VS Code for Data Science

Exploring Chinchilla Paper

let me start with the very basic understanding of functions, functions are basically ready written code which we can call whenever we need it to use. so as usual there are 2 types
The R Programming language considers these functions as an object and provides them the temporary access of interpreter at the time of execution. Once the function is done with the task provided, the control gets reverted to the interpreter as it was before.
R has a rich library of inbuilt functions which make life easy for an analyst. However, if you find any certain task can be automated using a function, you are free to write a function of your own under the R environment.
function_name - specifies the name of the function. You need to store a function somewhere.
argument_list - specifies the list of argument/s we use under a function.
expressions - specifies the expressions which get executed under a function body to have the required task done.
return -allows you to return a value for the defined function. If we don’t specify return while defining a function, the last line of code inside the function will get executed.
add <- function(a,b,c,d){
result = a + b + c + d
print(paste("addition is ", result))
}
add(1,2,3,4)
#Returns "addition is 10"
This can make your defined function more user-friendly. below function asks the user for input for values for variables the function becomes more generalized a, b, c since the user can provide the value of his interest for a, b, c.
# Function in R with user input
product <- function(a, b, c)
{
a <- readline("Please enter value for a: ")
b <- readline("Please enter value for b: ")
c <- readline("Please enter value for c: ")
#convert characters into integers
a <- as.integer(a)
b <- as.integer(b)
c <- as.integer(c)
product = a * b * c
print(paste("Product of given values is ", product))
}
product(a,b,c)
#Returns:
# product(a,b,c)
#Please enter value for a: 5
#Please enter value for b: 2
#Please enter value for c: 3
#"Product of given values is 30"
So we have seen how to write a function to automate some random tasks, like a calculator can do the same in no time, but to understand a bit about functions we should always start with a familiar field.
hope you are enjoying it, I am planning to write more often because while writing it makes things more clear so let's make something interesting in the next writing till then stay subscribed.