<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/statistics-sunday-reading-and-creating-a-data-frame-with-multiple-text-files/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.deeplytrivial.com/2018/11/statistics-sunday-reading-and-creating.html"> Deeply Trivial</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>First Statistics Sunday in far too long! It’s going to be a short one, but it describes a great trick I learned recently while completing a time study for our exams at work.</p>
<p>To give a bit of background, this time study involves analzying time examinees spent on their exam and whether they were able to complete all items. We’ve done time studies in the past to select time allowed for each exam, but we revisit on a cycle to make certain the time allowed is still ample. All of our exams are computer-administered, and we receive daily downloads from our exam provider with data on all exams administered that day.</p>
<p>What that means is, to study a year’s worth of exam data, I need to read in and analyze 365(ish – test centers are generally closed for holidays) text files. Fortunately, I found code that would read all files in a particular folder and bind them into a single data frame. First, I’ll set the working directory to the location of those files, and create a list of all files in that directory:</p>
<p><span>setwd(“Q:/ExamData/2018”)</span><br><span>filelist &lt;-list.files()</span><br><span><br></span>For the next part, I’ll need the data.table library, which you’ll want to install if you don’t already have it:</p>
<p><span>library(data.table)</span><br><span>Exams2018 &lt;-rbindlist(sapply(filelist, fread, simplify = FALSE, use.names = TRUE, idcol = “FileName”)</span><br><span><br></span>Now I have a data frame with all exam data from 2018, and an additional column that identifies which file a particular case came from.</p>
<p>What if your working directory has more files than you want to read? You can still use this code, with some updates. For instance, if you want only the text files from the working directory, you could add a regular expression to the list.files() code to only look for files with “.txt” extension:</p>
<p><span>list.files(pattern = “\\.txt$”)</span><br><span><br></span><span>If you’re only working with a handful of files, you can also manually create the list to be used in the rbindlist function. Like this:</span><br><span><br></span><span>filelist &lt;-c(“file1.txt”, “file2.txt”, “file3.txt”)</span><br><span><br></span><span>That’s all for now! Hope everyone has a Happy Thanksgiving!</span><br><span><br></span></p>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-3YG3bUQZ06k/We4DYIl5EpI/AAAAAAAAJnY/IHMK0Mmwg6YJJd6iZlCBxEl8Djz0djbqgCPcBGAYYCw/s1600/statistics_sunday.jpg?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/1.bp.blogspot.com/-3YG3bUQZ06k/We4DYIl5EpI/AAAAAAAAJnY/IHMK0Mmwg6YJJd6iZlCBxEl8Djz0djbqgCPcBGAYYCw/s320/statistics_sunday.jpg?resize=450%2C747&ssl=1" data-original-width="450" width="450" data-original-height="747" data-recalc-dims="1"></a></div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.deeplytrivial.com/2018/11/statistics-sunday-reading-and-creating.html"> Deeply Trivial</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/xVx-oirC9Ag" height="1"></a></div>