Mapply function in R

mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each … argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

Vectorize returns a new function that acts as if mapply was called.

Implemented Code for mapply

path = “your physical path”

dir.create(file.path(path), showWarnings = FALSE)
setwd(file.path(path)) #Setting working directory

library(openxlsx)
library(stringr)
library(plyr)

file_raw = list.files(pattern=”*.xlsx”) #Get the xlsx file names

#Read all xlsx files and keep them in a list of dataframes
data_sets <- lapply(file_raw, function(x)(read.xlsx(x,sheet = 1, colNames = TRUE)))

names(data_sets) <- #Give your names

#Mailers

library(foreign)

#keeping fields

column_func = function(x) {
v = x[c(“Your required columnnames”)]
}

datasets_mlrs = lapply(data_sets,column_func)

setwd(“./mailers”) #geting inside a folder in the working directory

#Exporting the list of df’s
lapply(1:length(datasets_mlrs), function(i) write.dbf(datasets_mlrs[[i]],
file = paste0(names(datasets_mlrs[i]), “.dbf”),factor2char = TRUE, max_nchar = 254))
file_am = list.files(pattern=”*.dbf”)

datasets_am <- lapply(file_am, function(x)(read.dbf(x,as.is = TRUE))) #Importing again

names(datasets_am) = paste0(gsub(“fm”,””,names(datasets_mlrs)),”am”) #Changing the data names

#Replacing NA by blank

datasets_am <- lapply(datasets_am, function(x) { x[is.na(x)] <- “” ; x })

#Adjusting fields due to mailers

name_list = names(datasets_am[[1]])[names(datasets_am[[1]])!=”Store_Numb”]

#The Dataframes in the list x and y are joined using the primary

datasets_joined <- mapply(function(x, y) merge(x, y, by.x=’Store.Number’,by.y=’Store_Numb’),
#Function for joining. Mapply applies the function on multiple lists

#Keeping only some columns in the dataframes within the list
x = lapply(datasets_cleaned,function(x) x[,!(names(x) %in% c(names(datasets_am[[1]])[names(datasets_am[[1]])!=”Store_Numb”]))]),
y = datasets_am, SIMPLIFY = F)

#Setting the data together
#do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Store_all = do.call(rbind.fill,datasets_joined)
#You get the joined dataframes stacked under each other

 

Author : Sayak Sengupta (email: sayak0505@gmail.com)

Leave a Reply

Close Menu