Error : Could Not Find Function “createDataPartition”

Were you trying to create a training & testing dataset using your CSV source using caret’s built in function createDataPartition ?

Here is the error message I received :

> inTrain <- createDataPartition(HRdata$Attrition,p=0.75,list = FALSE)
Error: could not find function "createDataPartition"

So we begin with the classic HR attrition data available for download here. You would need a kaggle login to download it. If you cannot download it for some reason, drop an email to me and I will be glad to handover the dataset to you.

So after setting the working directory in R we proceed to load the file into our variable :

> HRdata<-read.csv('Employee_Attrition.csv')

You now want to split the dataset into training and testing for building your ML model. Considering you don’t have the package named “caret”, you decide to install it using :

> install.packages('caret')

While installing pay close attention to the warnings, I am new to R – but in an open world I would ignore warnings and still compile the code and make it work. But the warnings received while installing the caret package may ( in our case certainly ) cause(es) issues.

This is the warning during installation and unpacking of the related package(s) which gets extracted and installed to successfully load caret :

package ‘mnormt’ successfully unpacked and MD5 sums checked
package ‘lava’ successfully unpacked and MD5 sums checked
package ‘numDeriv’ successfully unpacked and MD5 sums checked
Warning in install.packages :
 unable to move temporary installation ‘C:\Users\TAE\Documents\R\win-library\3.3\file33144f537d8a\numDeriv’ to ‘C:\Users\TAE\Documents\R\win-library\3.3\numDeriv’

I did the research and it looks like we will have to load few packages manually ( of those which R couldn’t automatically install while extracting packages needed for caret ).  Now this warning may vary from installation to installation. One reason can be the laptop I was using earlier had administrator and firewall policies which did not allow me to copy files (I got a lot of warnings). I tried it on another laptop which had administrator access and firewall overrides (I got less warnings ), strange that it works for some packages and for some it doesn’t. I will blog about this in detail as to why it happens after I do some more reading. For now, manual installation of the package, in our case numDeriv works fine using :

> install.packages("numDeriv")
Installing package into ‘C:/Users/TAE/Documents/R/win-library/3.3’(as ‘lib’ is unspecified)trying URL ''Content type 'application/zip' length 110553 bytes (107 KB)downloaded 107 KB
package ‘numDeriv’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in C:\Users\TAE\AppData\Local\Temp\RtmpWA8rt8\downloaded_packages

Without manually installing the package(s) (which were missed during the install.packages phase) if you try to load the caret library you will receive error(s). Moving on, since I installed numDeriv above I received another error for package rlang while I tried to load caret, you may receive many more – keep loading caret and install packages manually until all errors/packages are taken care of :

> library(caret)
Loading required package: lattice
Loading required package: ggplot2
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
 there is no package called ‘rlang’
In addition: Warning message:
package ‘caret’ was built under R version 3.3.3 
Error: package or namespace load failed for ‘caret’

By making sure no other packages were missing I manually installed all packages that were necessary ( issuing > library(caret) untill it’s all clear – if there is a better way, please leave the answer in the comments section below )

If the package or namespace is not fully loaded any built in function within the caret / any library for that matter will not work.

> library(caret)
Warning message:
package ‘caret’ was built under R version 3.3.3 
> set.seed(12345)
> inTrain <- createDataPartition(HRdata$Attrition,p=0.75,list = FALSE)

The above warning message is acceptable. I installed all the manual packages which were needed until I got no errors while trying to load the library caret as shown above and the error was fixed.