<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts | Sarah Berger</title><link>https://sarahberger.netlify.app/post/</link><atom:link href="https://sarahberger.netlify.app/post/index.xml" rel="self" type="application/rss+xml"/><description>Posts</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><image><url>https://sarahberger.netlify.app/images/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_2.png</url><title>Posts</title><link>https://sarahberger.netlify.app/post/</link></image><item><title>Introduction to data.table in R</title><link>https://sarahberger.netlify.app/post/intro-to-data-table/</link><pubDate>Sat, 05 Dec 2020 11:49:47 -0500</pubDate><guid>https://sarahberger.netlify.app/post/intro-to-data-table/</guid><description>&lt;p>One of the most exciting packages in the R universe is data.table. This package allows you to create data.table objects as an alternative to data.frame. The benefit of using data.table is the speed and efficiency when working with large datasets, and a concise syntax that can be really easy to use once you get used to it.&lt;/p>
&lt;h3 id="load-datatable-package-and-dataset">Load data.table package and dataset&lt;/h3>
&lt;p>Lets first start with installing and loading the package. For this demonstration we will use the iris dataset that is preinstalled in R.&lt;/p>
&lt;pre>&lt;code class="language-{r">install.packages(&amp;quot;data.table&amp;quot;)
library(data.table)
data = setDT(copy(iris))
#note we have to create a copy because iris is built-in to R and thus not modifiable
&lt;/code>&lt;/pre>
&lt;p>In this case we didn&amp;rsquo;t import our data from a csv file, but if you have a large dataset that you need to import, the &lt;code>fread()&lt;/code> function from data.table allows for extremely quick importing of data from either a csv file or a URL.&lt;/p>
&lt;p>Also, here we converted an existing data frame to a data table object using &lt;code>setDT()&lt;/code>. We could also use &lt;code>as.data.frame()&lt;/code> for an existing object, or create our own data table by inputting data using &lt;code>data.table()&lt;/code>&lt;/p>
&lt;h3 id="data-table-syntax">Data Table Syntax&lt;/h3>
&lt;p>The main syntax that data table uses is the &lt;code>DT[i, j, by]&lt;/code>&lt;/p>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/ijby_hu140ba4fc4e689957d016382bd5667147_27654_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/ijby_hu140ba4fc4e689957d016382bd5667147_27654_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="1142" height="402">
&lt;/a>
&lt;/figure>
&lt;p>This syntax allows for data to be manipulated extremely easily and in only one line of code! It may seem pretty foreign, so lets go through some explanations and examples.&lt;/p>
&lt;h4 id="filter-rows">Filter rows&lt;/h4>
&lt;p>Let&amp;rsquo;s say we only want the 15th row of our dataset. Or only data for flowers of the versicolor species. Or only data for the versicolor species with a sepal width less than 3. This is all easy to do in the &amp;ldquo;i&amp;rdquo; part of a data table.&lt;/p>
&lt;pre>&lt;code class="language-{r">#15th row
row_15 &amp;lt;- data[15]
row_15
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/row_15_hua600fa5f6b863cb70bced3df93f7147e_12328_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/row_15_hua600fa5f6b863cb70bced3df93f7147e_12328_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="449" height="35">
&lt;/a>
&lt;/figure>
&lt;pre>&lt;code class="language-{r"># versicolor species
versicolor &amp;lt;- data[Species == &amp;quot;versicolor&amp;quot;]
head(versicolor)
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/verisocolor_hu2b871f528f62e3d72baecba441b81abd_19437_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/verisocolor_hu2b871f528f62e3d72baecba441b81abd_19437_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="477" height="125">
&lt;/a>
&lt;/figure>
&lt;pre>&lt;code class="language-{r">#versicolor species with a sepal width less than 3
versicolor_sepal3 &amp;lt;- data[Species == &amp;quot;versicolor&amp;quot; &amp;amp; Sepal.Width &amp;lt; 3]
head(versicolor_sepal3)
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/verisocolor_sepal3_hu703f60949d9b78d70a152b898c790913_19721_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/verisocolor_sepal3_hu703f60949d9b78d70a152b898c790913_19721_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="470" height="119">
&lt;/a>
&lt;/figure>
&lt;h5 id="other-special-operators-include-in-and-between">Other special operators include %in% and %between%&lt;/h5>
&lt;pre>&lt;code class="language-{r">
setosa_virginica &amp;lt;- data[Species %in% c(&amp;quot;setosa&amp;quot;, &amp;quot;virginica&amp;quot;)]
pl_1.2_1.6 &amp;lt;- data[Petal.Length %between% c(1.2, 1.6)]
&lt;/code>&lt;/pre>
&lt;h4 id="working-with-columns">Working with columns&lt;/h4>
&lt;p>Let say you want to filter your dataset to contain all rows, but only the Species and Sepal Width columns. Note that you will leave the i section empty by just adding a comma before the j section.&lt;/p>
&lt;pre>&lt;code class="language-{r">
species_sepalw &amp;lt;- data[, c(&amp;quot;Species&amp;quot;, &amp;quot;Sepal.Width&amp;quot;)]
#an alternate way of writing a list in data.table is by using .()
species_sepalw &amp;lt;- data[, .(Species, Sepal.Width)]
head(species_sepalw)
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/species_sepalw_hu31d1435a71db3040ebe53149e620e1a3_13656_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/species_sepalw_hu31d1435a71db3040ebe53149e620e1a3_13656_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="441" height="123">
&lt;/a>
&lt;/figure>
&lt;p>It is also easy to do computations on columns in data.table. Let&amp;rsquo;s find the median sepal width&lt;/p>
&lt;pre>&lt;code class="language-{r">
median_sw &amp;lt;- data[, median(Sepal.Width)]
median_sw
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/median_sw_hubfd8e2d7933d1869ad5d4dcc3ea9c7d0_7082_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/median_sw_hubfd8e2d7933d1869ad5d4dcc3ea9c7d0_7082_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="454" height="19">
&lt;/a>
&lt;/figure>
&lt;h4 id="the--operator-creating-new-columns">The := Operator: Creating New Columns&lt;/h4>
&lt;p>Using the &amp;ldquo;:=&amp;rdquo; operator in the j section allows you to create new columns using existing ones. When creating columns by reference, you don&amp;rsquo;t have to assign the result to a new object, because the column will be directly added to the current data table. For example, let&amp;rsquo;s compute sepal area (length x width)&lt;/p>
&lt;pre>&lt;code class="language-{r">
data[, Sepal.Area := Sepal.Length * Sepal.Width]
head(data)
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/sepal.area_hu2c39a33b4f6ed02488b8dd0c4cacb03c_26569_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/sepal.area_hu2c39a33b4f6ed02488b8dd0c4cacb03c_26569_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="539" height="125">
&lt;/a>
&lt;/figure>
&lt;p>Notice how the new column name is on the left of the operator, and the operation is on the right.&lt;/p>
&lt;h4 id="group-by-variables">Group by variables&lt;/h4>
&lt;p>We can use the last section to group variables using &amp;ldquo;by&amp;rdquo;. For example, we can get the mean Sepal length by species.&lt;/p>
&lt;pre>&lt;code class="language-{r">
mean_by_species &amp;lt;- data[, .(mean.sepal.length = mean(Sepal.Length)), by = Species]
mean_by_species
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/mean_by_species_hu1ee9286990ce1143b4111245f64c46ac_14473_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/mean_by_species_hu1ee9286990ce1143b4111245f64c46ac_14473_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="537" height="72">
&lt;/a>
&lt;/figure>
&lt;p>You can also create a new column by using the := operator, that is grouped by a variable (or multiple variables).&lt;/p>
&lt;pre>&lt;code class="language-{r">
data[, mean.sepal.width := mean(Sepal.Width), by = Species]
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/mean.sepal.width_hu75580a7095716726c975800391489353_29996_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/mean.sepal.width_hu75580a7095716726c975800391489353_29996_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="645" height="124">
&lt;/a>
&lt;/figure>
&lt;h3 id="the-n-special-symbol">The .N special symbol&lt;/h3>
&lt;p>Using the .N symbol means the number of rows present. This can function similarly to nrows(). For example, here we can get the number of rows where the sepal width is less than 3 and sepal length is less than 5.&lt;/p>
&lt;pre>&lt;code class="language-{r">
less_3_5 &amp;lt;- data[Sepal.Width &amp;lt; 3 &amp;amp; Sepal.Length &amp;lt; 5, .N]
less_3_5
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/less_3_5_hu24189de4c975c86e3a7f8b64250b7211_7063_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/less_3_5_hu24189de4c975c86e3a7f8b64250b7211_7063_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="601" height="22">
&lt;/a>
&lt;/figure>
&lt;p>.N becomes really useful when you want to also use &amp;ldquo;by&amp;rdquo;. For example let&amp;rsquo;s get the number of observations per species.&lt;/p>
&lt;pre>&lt;code class="language-{r">
obs &amp;lt;- data[, .N, by = Species]
obs
&lt;/code>&lt;/pre>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/intro-to-data-table/obs_hu6d95c32b799e850435ee2eec7a056771_11952_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/intro-to-data-table/obs_hu6d95c32b799e850435ee2eec7a056771_11952_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="600" height="71">
&lt;/a>
&lt;/figure>
&lt;p>There are lots of other features of data.table that make it useful to use for data manipulation, but here I have gone over the basics and the operations that I use most often. Notice how easy it is to manipulate tables and combine multiple operations in only one line of code! If you are looking for more resources on using data.table you can:&lt;/p>
&lt;ol>
&lt;li>Visit the &lt;a href="https://cran.r-project.org/web/packages/data.table/data.table.pdf" target="_blank" rel="noopener">CRAN&lt;/a>&lt;/li>
&lt;li>Complete a datacamp tutorial. Here is one called &lt;a href="https://learn.datacamp.com/courses/data-manipulation-with-datatable-in-r" target="_blank" rel="noopener">Data Manipulation with data.table in R&lt;/a>&lt;/li>
&lt;li>Check out the CRAN Vignette called &lt;a href="https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html" target="_blank" rel="noopener">Introduction to data.table&lt;/a>&lt;/li>
&lt;/ol>
&lt;p>Happy analyzing!&lt;/p></description></item><item><title>The Importance of Reproducible Code</title><link>https://sarahberger.netlify.app/post/data-analysis-with-r/</link><pubDate>Mon, 30 Nov 2020 19:34:08 -0500</pubDate><guid>https://sarahberger.netlify.app/post/data-analysis-with-r/</guid><description>&lt;p>After I started conducting data analysis in R, I quickly realized that making your code easily reproducible is one of the most important steps to improving your research workflow. Trust me, I learned some of these lessons the hard way. The major benefits of making your code easy to understand and easy to reproduce are:&lt;/p>
&lt;ul>
&lt;li>You can share code easily with others and they will be able to run it and won&amp;rsquo;t be completely lost&lt;/li>
&lt;li>A few months down the line you can come back to your code and actually understand what the heck was going on&lt;/li>
&lt;li>You’re bound to make less errors&lt;/li>
&lt;li>It makes writing and reading code a much more pleasant experience&lt;/li>
&lt;/ul>
&lt;h3 id="my-top-tips-for-making-your-code-readable-and-reproducible-in-rstudio">My top tips for making your code readable and reproducible in RStudio:&lt;/h3>
&lt;ol>
&lt;li>Start by creating an R project (.RProj file) in a new folder on your computer. This allows you to have a separate directory for each project you work on. You can then add your script and any files you might need into the folder with the RProj and have a simple working directory. Then when loading files into your code, you can use relative file paths. If you need to share the project, you can easily zip it and send it to someone else, and all the necessary files will already be in there (with no need to change the file paths)!&lt;/li>
&lt;/ol>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/data-analysis-with-r/new.project_hu911bf1bbcd92d8b61b396ec6a2c2854c_33093_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/data-analysis-with-r/new.project_hu911bf1bbcd92d8b61b396ec6a2c2854c_33093_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="532" height="381">
&lt;/a>
&lt;/figure>
&lt;ol start="2">
&lt;li>
&lt;p>I like to use RMarkdown for my scripts. With RMarkdown you can integrate code “chunks” with plain text, making it easy to organize and explain your code. You can also “knit” the document to create either a PDF or an html file. A great resource for understanding RMarkdown is this &lt;a href="https://rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf" target="_blank" rel="noopener">cheat sheet&lt;/a>.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Make sure to load all packages at the top of your code. If you need to install the packages, make sure to delete or comment out that code.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;figure >
&lt;a data-fancybox="" href="https://sarahberger.netlify.app/post/data-analysis-with-r/load.packages_hu3f9f91e2c69863da37df3dc89e3ef622_23536_2000x2000_fit_lanczos_2.png" >
&lt;img data-src="https://sarahberger.netlify.app/post/data-analysis-with-r/load.packages_hu3f9f91e2c69863da37df3dc89e3ef622_23536_2000x2000_fit_lanczos_2.png" class="lazyload" alt="" width="388" height="193">
&lt;/a>
&lt;/figure>
&lt;ol start="4">
&lt;li>
&lt;p>Make sure your code runs in order. If you aren&amp;rsquo;t sure if your code runs in order, clear your global environment and restart your session and see if the script runs as a whole. It can sometimes be tempting to test stuff out and run things line by line, but this will make it much harder for you or anyone else to reproduce. Another benefit to using RMarkdown is that your document won’t knit properly unless it can run altogether.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Add comments to your code. This will make it so much easier for others and future you to understand what you were doing. I like to split my code up into sections and add a general explanation of what I am doing and why at the beginning of each section. Then within the body of the code, I will use #comments to explain the individual steps. You don’t need to comment literally every line, but make sure to annotate what is not obvious.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Try to make the names of your objects informative and intuitive. It&amp;rsquo;s also best not to make them too similar to each other in case you mix them up. For example having two tables called &lt;code>data_clean&lt;/code> and &lt;code>data_cleaner&lt;/code> will probably lead to some confusion.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>These are just some tips that I&amp;rsquo;ve found useful, but make sure to do what works for you! Good luck and happy coding 😄&lt;/p></description></item></channel></rss>