<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Demo | Gulsah Gurkan</title><link>https://www.gulsahgurkan.com/tag/demo/</link><atom:link href="https://www.gulsahgurkan.com/tag/demo/index.xml" rel="self" type="application/rss+xml"/><description>Demo</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Tue, 05 Oct 2021 00:00:00 +0000</lastBuildDate><image><url>https://www.gulsahgurkan.com/media/icon_hu457ec955eb1ae6b91e3c2a5a0294165b_258564_512x512_fill_lanczos_center_2.png</url><title>Demo</title><link>https://www.gulsahgurkan.com/tag/demo/</link></image><item><title>Let's start with automating reading data files in R!</title><link>https://www.gulsahgurkan.com/post/autoread-files/</link><pubDate>Tue, 05 Oct 2021 00:00:00 +0000</pubDate><guid>https://www.gulsahgurkan.com/post/autoread-files/</guid><description>&lt;p>Sometimes you need to read in the data file (or more importantly file&lt;strong>s&lt;/strong>) from a folder without knowing what they are named. If it is a single file you need for just one time, you may think: “OK, I’ll just get the file path. What is the big deal?!” But, you may be in a situation where the data file gets updated consistently with some tweaks in its name (e.g., the date that the data was updated on, initials of the person uploading it etc.) and you need to account for that change in the file path in your code. Another situation may be when reading in multiple data files from the same folder. In that case, you may be reading in each file separately, one by one. Do I hear: “Repeating almost the same line of code multiple times, I hate that!”&lt;/p>
&lt;p>Well, you are in luck. Try this:&lt;/p>
&lt;pre>&lt;code class="language-r"># this is the path to the folder you’d like to read the data files in.
fpath &amp;lt;- “…”
# this is where you get the names of all the files in that folder.
dfnames &amp;lt;- list.files(fpath)
&lt;/code>&lt;/pre>
&lt;p>Let’s read in the file that is alphabetically the first in the folder. Assuming you know the file format, let&amp;rsquo;s say it is a .csv file:&lt;/p>
&lt;pre>&lt;code class="language-r">df &amp;lt;- readr::read_csv(paste0(fpath, &amp;quot;\\&amp;quot;, dfnames[1]))
&lt;/code>&lt;/pre>
&lt;p>👉 Be sure to put the separator that is consistent with your fpath format (could be either “\\” or “/”).&lt;/p>
&lt;p>Now, if you would like to read in multiple files with the same file type from the same folder at once into a list, you can try this:&lt;/p>
&lt;pre>&lt;code class="language-r"># Create an empty list to append the data files
dflist &amp;lt;- list()
# Read in the csv files only
for(i in dfnames[stringr::str_detect(dfnames, &amp;quot;.csv&amp;quot;)]){
tt &amp;lt;- readr::read_csv(paste0(fpath,&amp;quot;\\&amp;quot;,i))
dflist[[i]] &amp;lt;- tt
}
&lt;/code>&lt;/pre>
&lt;p>Note that reading the name of the files from the folder directly gives you the opportunity to detect the file type and read them in accordingly. You can conditionally read the files in to account for each file type in the folder. For example, you can conditionally read the data files that are either .csv or .xlsx like this:&lt;/p>
&lt;pre>&lt;code class="language-r"># Create an empty list to append the data files
dflist &amp;lt;- list()
# Read in the data files based on type
for(i in dfnames){
if(stringr::str_detect(i, &amp;quot;.csv&amp;quot;)){
tt &amp;lt;- readr::read_csv(paste0(fpath,&amp;quot;\\&amp;quot;,i))
dflist[[i]] &amp;lt;- tt
}else if(stringr::str_detect(i, &amp;quot;.xlsx&amp;quot;)){
tt &amp;lt;- readxl::read_xlsx(paste0(fpath,&amp;quot;\\&amp;quot;,i))
dflist[[i]] &amp;lt;- tt
}
}
&lt;/code>&lt;/pre>
&lt;p>💡 Bonus:&lt;/p>
&lt;p>Do you need to read in data file/s from a zipped file? Here is a lifesaver using the unzip() and unz() functions to unzip files:&lt;/p>
&lt;pre>&lt;code class="language-r"># read in a data file (&amp;quot;my_data.csv&amp;quot;) from a zipped folder (&amp;quot;my_zipped_file.zip&amp;quot;):
filenames_zipped &amp;lt;- as.character(unzip(&amp;quot;...\\my_zipped_file.zip&amp;quot;, list = TRUE)$Name)
my_data &amp;lt;- readr::read_csv(unz(&amp;quot;...\\my_zipped_file.zip&amp;quot;, &amp;quot;my_data.csv&amp;quot;))
&lt;/code>&lt;/pre>
&lt;p>If you would like to see an example of these types of automation in full action, check out this R script from a project that I worked on a while ago:&lt;/p>
&lt;p>👉 &lt;a href="https://github.com/Gulsah-G/CCD_data_prep" target="_blank" rel="noopener">&lt;strong>Pre-processing NCES Common Core of Data&lt;/strong>&lt;/a>&lt;/p></description></item></channel></rss>