<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/clean-your-data-in-seconds-with-this-r-function/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/clean-your-data-in-seconds-with-this-r-function/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>All data needs to be clean before you can explore and create models. Common sense, right. Cleaning data can be tedious but I created a function that will help. </p>
<p>The function do the following: </p>
<ul>
<li>Clean Data from NA’s and Blanks</li>
<li>Separate the clean data – Integer dataframe, Double dataframe, Factor dataframe, Numeric dataframe, and Factor and Numeric dataframe.</li>
<li>View the new dataframes</li>
<li>Create a view of the summary and describe from the clean data.</li>
<li>Create histograms of the data frames.</li>
<li>Save all the objects </li>
</ul>
<p>This will happen in seconds.</p>
<h3>Package</h3>
<p>First, load <code>Hmisc</code> package. I always save the original file.<br>
The code below is the engine that cleans the data file. </p>
<pre>cleandata &lt;- dataname[complete.cases(dataname),] </pre>
<h3>The function</h3>
<p>The function is below. You need to copy the code and save it in an R file. Run the code and the function <code>cleanme</code> will appear. </p>
<pre>
cleanme &lt;- function(dataname){
  
  #SAVE THE ORIGINAL FILE
  oldfile &lt;- write.csv(dataname, file = &quot;oldfile.csv&quot;, row.names = FALSE, na = &quot;&quot;)
  
  #CLEAN THE FILE. SAVE THE CLEAN. IMPORT THE CLEAN FILE. CHANGE THE TO A DATAFRAME.
  cleandata &lt;- dataname[complete.cases(dataname),]
  cleanfile &lt;- write.csv(cleandata, file = &quot;cleanfile.csv&quot;, row.names = FALSE, na = &quot;&quot;)
  cleanfileread &lt;- read.csv(file = &quot;cleanfile.csv&quot;)
  cleanfiledata &lt;- as.data.frame(cleanfileread)
  
  #SUBSETTING THE DATA TO TYPES
  logicmeint &lt;- cleanfiledata[,sapply(cleanfiledata,is.integer)]
  logicmedouble &lt;- cleanfiledata[,sapply(cleanfiledata,is.double)]
  logicmefactor &lt;- cleanfiledata[,sapply(cleanfiledata,is.factor)]
  logicmenum &lt;- cleanfiledata[,sapply(cleanfiledata,is.numeric)]
  mainlogicmefactors &lt;- cleanfiledata[,sapply(cleanfiledata,is.factor) | sapply(cleanfiledata,is.numeric)]

  #VIEW ALL FILES
  View(cleanfiledata)
  View(logicmeint)
  View(logicmedouble)
  View(logicmefactor)
  View(logicmenum)
  View(mainlogicmefactors)
  
  #describeFast(mainlogicmefactors)
  
  #ANALYTICS OF THE MAIN DATAFRAME
  cleansum &lt;- summary(cleanfiledata)
  print(cleansum)
  cleandec &lt;- describe(cleanfiledata)
  print(cleandec)
  
  #ANALYTICS OF THE FACTOR DATAFRAME
  factorsum &lt;- summary(logicmefactor)
  print(factorsum)
  factordec &lt;- describe(logicmefactor)
  print(factordec)
  
  #ANALYTICS OF THE NUMBER DATAFRAME
  numbersum &lt;- summary(logicmenum)
  print(numbersum)
  
  numberdec &lt;- describe(logicmefactor)
  print(numberdec)
  
  mainlogicmefactorsdec &lt;- describe(mainlogicmefactors)
  print(mainlogicmefactorsdec)
  
  mainlogicmefactorssum &lt;- describe(mainlogicmefactors)
  print(mainlogicmefactorssum)
  
  #savemenow &lt;- saveRDS(&quot;cleanmework.rds&quot;)
  #readnow &lt;- readRDS(savemenow)
  
  #HISTOGRAM PLOTS OF ALL TYPES
  hist(cleanfiledata)
  hist(logicmeint)
  hist(logicmedouble)
  hist(logicmefactor)
  hist(logicmenum)
  #plot(mainlogicmefactors)

  save(cleanfiledata, logicmeint, mainlogicmefactors, logicmedouble, logicmefactor, logicmenum, numberdec, numbersum, factordec, factorsum, cleandec, oldfile, cleandata, cleanfile, cleanfileread,   file = &quot;cleanmework.RData&quot;)
}</pre>
<p>Type in and run:</p>
<pre>cleanme(dataname)</pre>
<p>When all the data frames appear, type to load the workspace as objects. </p>
<pre>load(&quot;cleanmework.RData&quot;)</pre>
<p>Enjoy </p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/hands-on-tutorial-on-python-data-processing-library-pandas-part-2/" title="Permanent Link toHands-on Tutorial on Python Data Processing Library Pandas – Part 2" target="_blank">Hands-on Tutorial on Python Data Processing Library Pandas – Part 2</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/hands-on-tutorial-on-python-data-processing-library-pandas-part-1/" title="Permanent Link toHands-on Tutorial on Python Data Processing Library Pandas – Part 1" target="_blank">Hands-on Tutorial on Python Data Processing Library Pandas – Part 1</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/using-r-with-monetdb/" title="Permanent Link toUsing R with MonetDB" target="_blank">Using R with MonetDB</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/recording-and-measuring-your-musical-progress-with-r/" title="Permanent Link toRecording and Measuring Your Musical Progress with R" target="_blank">Recording and Measuring Your Musical Progress with R</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/spark-rdds-vs-dataframes-vs-sparksql-part-4-set-operators/" title="Permanent Link toSpark RDDs Vs DataFrames vs SparkSQL – Part 4 Set Operators" target="_blank">Spark RDDs Vs DataFrames vs SparkSQL – Part 4 Set Operators</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/clean-your-data-in-seconds-with-this-r-function/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/lHuebEaRDVk" height="1"></a></div>