Overview

R is a free, open-source programming language commonly used in the fields of economics, statistics, finance, medicine, sociology, …. It is great for academic research because it is excellent at data manipulation, calculation, and graphical display. Researchers like you can consider R as a more advanced and powerful but convenient version of Excel. It is available for download at https://cloud.r-project.org/, where you can choose and download the R that match your computer system.

RStudio is a free open source integrated development environment (IDE) for R. Basically, it is a program that provides a nice graphical interface for programming R. It is available for download at https://www.rstudio.com/products/rstudio/download/, where you should select and download RStudio Desktop, the free version of RStudio.

The objective of this introduction section is to introduce you the basics of R and RStudio, this includes

For additional resources, you can consult the online reference R for Data Science at http://r4ds.had.co.nz.

Getting to know RStudio

After first opening RStudio, there should be three panes in your window

There is an additional pane that likely won’t be present at first, the Editor pane, which will take up the top-left quadrant of the window above the Console pane; this is where you’ll view and write your scripts.

Note: The layout can be fully customized through the Preferences menu.

Projects and Working Directory

Often when programming and conducting research, you want to organizing everything related to work into one single folder and organize them in a consistent way. RStudio makes organizing files projects very easy. You can create a new project

You can set up a new directory/folder for your project or choose a existing folder. Creating a project will create a .Rproj in the folder you created or chose; when opened, this file will open a new instance of RStudio and load that project’s scripts, console history, …. Using project files in RStudio makes stopping and resuming projects (and sharing them) very easy.

In the project folder, along with your .Rproj file, it is also a good idea to create subfolders for organizing your project’s related files; some common subfolders are data and figures.

Installing and Loading packages

The capabilities of R can be greatly extended through the use packages. In fact, using packages is much more easier than designing and composing your own codes; existing packages are more than sufficient for statistical analyses.

The installation of new packages can be done directly through RStudio using the function install.packages(). For every package, you only need to install once.

One of the most useful packages is tidyverse, which is a collection of many very useful packages. To install the package, type

install.packages("tidyverse")

After installing a package, you will also need to load the package in order to use it, you can do this by typing library(package name). You need to load a package that you installed every time you start a new (re)session. For example, if you want to load the tidyverse package installed before, run

library(tidyverse)

Fun fact: some packages in R are built-in data frames that designed for users to practice coding in R.

Importing Data

For your research project, you will use your own data instead of a built-in data frame; how you import this data depends on its file type.

To import your data, first make sure the data in your file is properly formatted. That means rows are observations and columms are variables. Assuming there are not any issues, your data from your file will then be imported and stored under “Environment” as a data frame.

Note: If you have data in unusual formats you’ll likely need to give some more information to R letting it know how to read in your data; see Section 11 in R for Data Science or the function documentation for more information.

Data frames

A data frame is the most basic data structure in R, and takes the form of a table or two-dimensional array-like structure.

You can quickly get information on the structure of your data frame using function str(framename), and quickly get summary statistics by using the function summary(framename).

Scripts

As mentioned above, the Editor pane is where you will see and write your scripts, which will be the main way you program in R. A script is a collection of code (and comments) that allows you repeat complicated task step by step. Writing scripts will also make it much less likely you make a mistake, and will make mistakes much easier to find and correct. Generally, when doing minor testing you use the console; when writing more serious programs you write a script.

You can open a new script

This will create a new .R file and you can save it in your working directory.

Writing good scripts makes your research process easier, trackable, and repeatable. When writing scripts for your research, you should always include comments describing what your code does. If you do not include a comment, it will be very hard to understand what your script does so that others cannot help you and you may also get confused about your script.

You should also always start your scripts with a header containing information like the name of your script, when it was written, who wrote it, and what it does.

RMarkdown

Markdown is a text-to-HTML coversion tool for webwriters. Developers of RStudio designed a similar tool called RMarkdown, where you can write scripts, add comments, and convert your writing into HTML, WORD, or PDF formats. You can create a new Rmarkdown in the way that is similiar to creatig a R script file. This will give you a .Rmd file.

When you write in Rmarkdown, you do not need to type # for commments. # is used for titles and subtitles. However, to write codes, you need to insert chunks, which provides a clearer visual seperation of your codes and aoosciated comments. You can convert your RMarkdown file into aforementioned file types by cliking “knit.”

Note: To convert into PDF files, you must have LaTeX installed in your computer. You need to have both a TeX distribution and a TeX editor on your computer. For Tex distribution, you can use MiKTeX from https://miktex.org/download, and you can get Texmaker from https://www.xm1math.net/texmaker/download.html as a Tex editor.