Creating Functions in R
Functions
Functions are one of the most widely used concepts in programming paradigm. Functions ,also called module, can help us to automate a repetitive task and reduce time and effort. In this Blog, we will look at how to create functions in R. We will go form simple examples to complex ones and also discuss …(three dot notation) concept
Basic syntax of a function
<- function(arg1, arg2, ... ){
myfunction
statementsreturn(object)
}
Function to calculate Product of two numbers
<-function(x,y){
func_product
<-x*y
zreturn(z)
}
func_product(2,3)
[1] 6
Function to calculate Product of n numbers
Using 3 dot notation(…) we will be able to create functions that can accept any number of arguments without having to explicitly mention them while defining the function itself
We will also be installing purrr package as it will be required to check if a list is empty or not
if(!require("purrr")){
install.packages("purrr")
else{
}
library(purrr)
}
# Declaring the function
<-function(x,y,...){
func_product_n
<-x*y
z
# Storing elements other than x and y
<-list(...)
params
# Checking if params is empty
if(is_empty(params)){
<-1
temp
else{
}
for(i in params){
<-z*i
z
}# End of If statement
}
return(z)
}
func_product_n(1,2,3,4,5)
[1] 120
This is a very simple yet powerful example to show how we can pass dynamic arguments in R. It is similar to ‘args’ used extensively in python
Lets see some more examples
Write a function to remove NA from a vector
<-c(1,2,3,4,NA,4,5,6,7)
s<-function(x){
func_Remove_NA
<-!is.na(x)
pos<-x[pos]
yreturn(y)
}
func_Remove_NA(s)
[1] 1 2 3 4 4 5 6 7
Write a function to replace NA with 0 within a vector
<-c(1,2,3,4,NA,4,5,6,7)
s<-function(x){
func_Replace_NA
<-is.na(x)
pos<-0
x[pos]return(x)
}
func_Replace_NA(s)
[1] 1 2 3 4 0 4 5 6 7
Lets create a function that read all the csv files from a folder
<-function(x){
read.csv.files
# Getting all the csv files within the folder
<-c(".csv")
input.file.type<- list.files(x,pattern=input.file.type)
csv.files
# Reading a csv files into a list
<-list()
l1
for(i in csv.files){
<-read.csv(paste0(x,"\\",i),stringsAsFactors=F)
l1[[i]]
}
print(paste0("The names of the csv files imported form the folder are:"))
print(csv.files)
return(l1)
}
Here the argument supplied to the function will be the folder path.Lets use this to read all the csv files from the ‘Parag’ folder within Documents main folder other
<-paste0(getwd(),"\\Parag")
folder_path<-read.csv.files(folder_path) l2
[1] "The names of the csv files imported form the folder are:"
[1] "file2.csv"
Lets check one of the csv files
names(l2)[1]
[1] "file2.csv"
names(l2)[1] gives the name of the first csv file stored in l2.Lets read it into a data frame element df2
<-names(l2)[1]
nms<-l2[[nms]]
df2head(df2)
No Comment Type
0 he said:wonderful. A
1 The problem is: reading table, and also a problem yes. keep going on. A
Creating a function to standardise file names
As we can see that the names of the files have certain numbers apart from the text. For instance, in ‘CLI_16572948.csv’, there is text separated from numbers by a ’_‘. These numbers can be related to a time stamp and are appended to file names when the files are generated by a ’job’(normally in a production environment). We need to to remove them to do any further analysis
if(!require("stringr")){
install.packages("stringr")
else{
}
library(stringr)
}
<-function(x){
function_standardise_names
<-list()
ls.names
for(i in names(l2)){
<-str_split(i,"_")[[1]][1]
new.nms<-new.nms
ls.names[[new.nms]]
}return(names(ls.names))
}
<-function_standardise_names(l2)
correctd.nms correctd.nms
[1] "file2.csv"
Assigning correctd.nms to l2
names(l2)<-correctd.nms
names(l2)
[1] "file2.csv"
Final Comments
We saw that it becomes necessary to create functions in order to perform repetitive task. It not only saves time but also lends certain structure to the entire code. We also went through examples where …(3 dot notation) notation was used to make the function more dynamic. Understanding of the functions is very important as in order to create a library in R, we need to create function first. Library is created when we want to share our results
Link to Previous R Blogs
Blog 1-Vectors,Matrics, Lists and Data Frame in R https://mlmadeeasy.blogspot.com/2019/12/2datatypesr.html
Blog 2 - Operators in R https://mlmadeeasy.blogspot.com/2019/12/blog-2-operators-in-r.html
Blog 3 - Loops in R https://mlmadeeasy.blogspot.com/2019/12/blog-3-loops-in-r.html
Blog 4 - Indexing in R https://mlmadeeasy.blogspot.com/2019/12/blog-4-indexing-in-r.html
Blog 5- Handling NA in R https://mlmadeeasy.blogspot.com/2019/12/blog-5-handling-na-in-r.html
Blog 6- Handling NA in R https://mlmadeeasy.blogspot.com/2019/12/blog-6tips-to-generate-plots.html
List of Datasets for Practise https://hofmann.public.iastate.edu/data_in_r_sortable.html
No comments:
Post a Comment