16 Thesis Success with R-Studio: A Step-by-Step Tutorial and Practical Analysis Examples

This comprehensive tutorial is designed specifically for thesis students such as yourself, eager to harness the power of R programming to elevate your thesis project. Whether you’re new to coding or looking to enhance your skills, this guide will walk you through the fundamentals of R, empowering you to confidently tackle basic analyses and create impactful visualizations. From data manipulation to insightful graphs, you’ll gain the essential tools needed to enhance the depth and rigour of your research. Join us on this exciting learning adventure as we equip you with the skills to navigate R’s landscape, unravel complex datasets, and transform your thesis into a compelling narrative backed by robust data analysis. Let’s dive in and unlock the full potential of R for your academic success!

This document can act as a stand-alone guide that you read through at your own pace.

Preparing Your Work Space

Let’s begin by downloading and loading all the required libraries!

R packages or libraries enhance R’s capabilities. You can think of them as a tool box that carries a specific set of tools or in this case code functions for specific jobs. Here I’ve listed some basic ones that will suffice for the scope of this exercise.

You can install a package with:

install.packages("package_name")

You can load and use the package with:

library(package_name)

This grants you access to its functions However, instead of running these two functions over and over again for each library, I use the code included below. I find it to be especially useful when I share my code with others. This code checks to see if any of the packages specified in the list “my_packages” is downloaded or not, and if a package is not installed, then proceeds to download it.

# List your required packages
my_packages <- c("tidyverse", "rstatix", "readxl", "xlsx", "effectsize", "emmeans", "kableExtra", "grid", "gridExtra", "ggpubr", "ggplot2", "ggsci", "cowplot", "devtools")

# Extract packages from the list that are not already installed
not_installed <- my_packages[!(my_packages %in% installed.packages()[ , "Package"])] 

# Install packages that are not already installed
if(length(not_installed)) install.packages(not_installed)

# Load the required libraries
library(tidyverse)    # for data manipulation 
library(rstatix)      # for statistical analyses
library(readxl)       # to read excel files
library(xlsx)         # to create excel files
library(effectsize)   # to calculate measures of effect size
library(kableExtra)   # formatting printable html ANOVA tables 
library(ggpubr)       # for making plots & figures
library(ggplot2)      # for making plots & figures
library(grid)         # for organizing multiple plots 
library(ggsci)        # for Scientific Journal and Sci-Fi Themed Color Palettes for ggplot2
library(gridExtra)    # to arrange multiple grid-based plots on a page
library(cowplot)      # for making plots and figures; an add-on to ggplot

Writing and Running R Code: Writing code in RStudio is simple. You can do so in the console, by typing the code then hitting the “return’ button. Or here in the source. To run one line of code, place your cursor on it and press Ctrl + Enter (Windows/Linux) or Command + return (macOS). You can also run multiple lines together by highlighting the lines and hitting the keys. There’s also the run option at the top which tells you which shortcut keys correspond to the command. Nevertheless, if you forget the keys, you can use the run option above. To run an entire Chunk of code and see the output, hit the little green arrow beside it.

Tips (Basic R Syntax):

  • You can assign values to variables using the <- operator.
  • Use the # symbol to create a single-line comment.
  • Anything following the # symbol on the same line is considered a comment and is not executed by R.

Setting Up Your Working Directory

With our libraries secured and loaded, we need to set up our working directory and load our dataset. Please download the required dataset for these R exercises by clicking on this link.

On your computer create a folder where you will store all of the datasets for this exercise. Save your current R session to this folder as well.

Next, we must set the working directory of our current R studio session to this folder. This is also the folder where we would want the results of our analyses exported. You may set up your working directory with code using the function “setwd()”, which requires you to input the full directory address, which is hard to find, especially on an Apple device. But we can pair it with another function, “setwd(file.choose())” which allows us to pick from our desktop the folder we wish to set as our working directory. The output of this function will be the full file path, that you can use in the “setwd()” function to avoid picking your file in future runs of the same code.

You can also set your working directory through RStudio’s menu. At the top of the screen click on the “Session” tab, then among the options hover over “Set Working Directory” then pick “Choose Directory” and simply choose your designated file with the datasets.

Once you do this, in the Console, the output you see will be the path to your file which you can copy and paste into the “setwd()” code so you can avoid the manual work the next time you run through your code.

Tip: When naming files, use underlines “_” instead of empty spaces. This makes it easier to load data by name. 

License

Share This Book