-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
771 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# options specified here will apply to all posts in this folder | ||
|
||
# freeze computational output | ||
# (see https://quarto.org/docs/projects/code-execution.html#freeze) | ||
freeze: true | ||
|
||
# Enable banner style title blocks | ||
title-block-banner: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: "Getting Started with R and RStudio" | ||
subtitle: "Software Installation" | ||
author: "William Okech" | ||
date: "2022-06-08" | ||
image: "r_and_rstudio.png" | ||
categories: [RStudio, R, Tutorial, Blog] | ||
toc: true | ||
draft: false | ||
--- | ||
|
||
# Welcome! | ||
|
||
In this 1st post, the reader will be introduced to the R programming language and RStudio software. | ||
|
||
## Introduction | ||
|
||
This blog aims to introduce new R/RStudio users to the fundamentals of R and lay the groundwork for more in-depth statistical analysis, data visualization, and reporting methods. I hope to present the topics in a straightforward manner so that anyone new to programming is not intimidated. | ||
|
||
## What is R? | ||
|
||
R is a programming language and open-source (freely available) software invented by Ross Ihaka and Robert Gentleman in 1993 (published as open-source in 1995) when they were based at the University of Auckland. *Fun fact: R represents the first letter of the first names of the creators*. The software is utilized by individuals working for various organizations ranging from academic institutions and healthcare organizations to financial services and information technology companies. In May 2022, the [TIOBE](https://www.tiobe.com/tiobe-index/) index (a measure of programming language popularity) demonstrated that R was the 13th most popular programming language. R's popularity may result from its highly extensible nature that allows users to perform statistical data analysis, generate visualizations, and report findings. | ||
|
||
## What are the benefits of using R? | ||
|
||
As mentioned in the previous section, R is an open-source software that is highly extensible. Thousands of extensions (also known as packages) can be installed, allowing one to increase the number of available applications. The main advantages of R include: 1. A [large](https://community.rstudio.com/) [community](https://www.r-project.org/help.html) of [users and developers](https://stackoverflow.com/) that can provide learning support and assist with technical challenges, 2. The ability to perform reproducible research. 3. Its cross-platform nature, which means that it can be used on Linux, Windows, and Mac operating systems. 4. The ability to generate [high-quality](https://r-graph-gallery.com/) [graphics](https://r-charts.com/) from datasets of varying dimensions. | ||
|
||
## I'm looking for R. Where can I get it? | ||
|
||
To install R on your personal computer, visit The R Project for Statistical Computing's Comprehensive R Archive Network [(CRAN)](https://cran.r-project.org/), download the most recent version, and install it according to the website's instructions. Once you download R, you can now experiment with some of its features. | ||
|
||
{fig-align="center" width="90%"} | ||
|
||
Figure 1: The standard R interface (Windows) | ||
|
||
When you open R, you will notice that it has a basic graphical user interface (GUI), and the console displays a command-line interface (CLI; where each command is executed one at a time). This may be intimidating for new users; however, there is a workaround for those who are not comfortable working at the command line. For those who are not experienced programmers, R can be used with an application called RStudio. | ||
|
||
## What is RStudio and how does it differ from R? | ||
|
||
RStudio is an integrated development environment (IDE) for R that was developed by JJ Allaire. This software contains tools that make programming in R easier. RStudio extends R's capabilities by making it easier to import data, write scripts, and generate visualizations and reports. The RStudio IDE is available for download from the [RStudio](https://www.rstudio.com/products/rstudio/download/) website. | ||
|
||
{fig-align="center" width="90%"} | ||
|
||
Figure 2: RStudio interface with four main panes (Windows) | ||
|
||
Once installed, the basic layout of RStudio reveals that there is a script (text editor), console, navigation, and environment/history window pane. The script pane (text editor) in the upper-left allows one to write, open, edit, and execute more extended programs compared with using the standalone R software. The console pane (bottom-left) displays the script's output and offers a command-line interface for typing code that is immediately executed. The environment pane (upper-right) displays information about the created objects, the history of executed code, and any external connections. Finally, the navigation pane (bottom-right) shows multiple tabs. Its primary tabs include the "Plot" tab, which shows graphics created by code, the "Packages" tab where the packages are installed, and the "Help" tab, which provides assistance for all things R and allows one to search the R documentation. | ||
|
||
## What are the primary benefits of RStudio? | ||
|
||
RStudio allows one to create projects (a collection of related files stored within a working directory). Additionally, RStudio can be customized using options available under the "Tools" tab. Lastly, RStudio has Git integration that allows for version control where you can back up your code at different timepoints and effortlessly transfer code between computers.[^1] | ||
|
||
[^1]: Summary of the benefits of R and RStudio obtained from Lander, J. P. (2017). R for everyone: Advanced analytics and graphics. Addison-Wesley. | ||
|
||
## Conclusion | ||
|
||
Hopefully, this was a helpful introduction to R and RStudio. In subsequent blog posts, we will focus on: | ||
|
||
1. [Part 1: Simple arithmetic](/posts/series_1/new_post_2/post_2.html), | ||
2. [Part 2: Variables](/posts/series_1/new_post_3/post_3.html), | ||
3. [Part 3: Data types](/posts/series_1/new_post_4/post_4.html), | ||
4. [Operators](), | ||
5. [Vectors](), | ||
6. [Missing data]() | ||
|
||
## |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
--- | ||
title: "The Basics of R and RStudio" | ||
subtitle: "Part 1: Simple Arithmetic" | ||
author: "William Okech" | ||
date: "2022-06-15" | ||
image: "r_and_rstudio.png" | ||
categories: [RStudio, R, Tutorial, Blog] | ||
toc: true | ||
draft: false | ||
--- | ||
|
||
## Introduction | ||
|
||
This is the first in a series of blog posts looking at the basics of R and RStudio. These programs allow us to perform various basic and complex calculations. To get started, first, we will open R or RStudio. In R, go to the console, and in RStudio, head to the console pane. Next, type in a basic arithmetic calculation such as "1 + 1" after the angle bracket (\>) and hit "Enter." | ||
|
||
An example of a basic calculation: | ||
|
||
```{r} | ||
1+1 | ||
``` | ||
|
||
The output will be observed next to the square bracket containing the number 1 (\[1\]). | ||
|
||
{fig-align="center" width="90%"} | ||
|
||
Additionally, to include comments into the code block we use the hash (#) symbol. Anything written after the code block will be commented out and not run. | ||
|
||
```{r} | ||
# A simple arithmetic calculation (which is not run because of the hash symbol) | ||
1+1 | ||
``` | ||
|
||
## What arithmetic operators are available in R/RStudio? | ||
|
||
Various arithmetic operators (listed below) can be used in R/RStudio. | ||
|
||
| Arithmetic Operator | Description | | ||
|:-------------------:|:----------------------------------:| | ||
| \+ | Addition | | ||
| \- | Subtraction | | ||
| \* | Multiplication | | ||
| / | Division | | ||
| \*\* or \^ | Exponentiation | | ||
| %% | Modulus (remainder after division) | | ||
| %/% | Integer division | | ||
|
||
## Examples | ||
|
||
Addition | ||
|
||
```{r} | ||
10+30 | ||
``` | ||
|
||
Subtraction | ||
|
||
```{r} | ||
30-24 | ||
``` | ||
|
||
Multiplication | ||
|
||
```{r} | ||
20*4 | ||
``` | ||
|
||
Division | ||
|
||
```{r} | ||
93/4 | ||
``` | ||
|
||
Exponentiation | ||
|
||
```{r} | ||
3^6 | ||
``` | ||
|
||
Modulus (remainder with division) | ||
|
||
```{r} | ||
94%%5 | ||
``` | ||
|
||
Integer Division | ||
|
||
```{r} | ||
54%/%7 | ||
``` | ||
|
||
Slightly more complex arithmetic operations | ||
|
||
```{r} | ||
5-1+(4*3)/16*3 | ||
``` | ||
|
||
## Summary | ||
|
||
After going through this post, I hope that you can now perform simple arithmetic calculations in R/RStudio. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
title: "The Basics of R and RStudio" | ||
subtitle: "Part 2: Variables" | ||
author: "William Okech" | ||
date: "2022-06-22" | ||
image: "r_and_rstudio.png" | ||
categories: [RStudio, R, Tutorial, Blog] | ||
toc: true | ||
draft: false | ||
--- | ||
|
||
## Introduction | ||
|
||
Variables are instrumental in programming because they are used as "containers" to store data values. To assign a value to a variable, we can use `<−` or `=`. However, most R users prefer to use `<−`. | ||
|
||
Examples of variable assignment | ||
|
||
1. Using `<-` | ||
|
||
```{r} | ||
variable_1 <- 5 | ||
variable_1 | ||
``` | ||
|
||
2. Using `=` | ||
|
||
```{r} | ||
variable_2 = 10 | ||
variable_2 | ||
``` | ||
|
||
3. Reverse the value and variable with `->` | ||
|
||
```{r} | ||
15 -> variable_3 | ||
variable_3 | ||
``` | ||
|
||
4. Assign two variables to one value | ||
|
||
```{r} | ||
variable_4 <- variable_5 <- 30 | ||
variable_4 | ||
variable_5 | ||
``` | ||
|
||
The output of the variable can then be obtained by: | ||
|
||
1. Typing the variable name and then pressing "Enter," | ||
2. Typing "print" with the variable name in brackets, `print(variable)`, and | ||
3. Typing "View" with the variable name in brackets, `View(variable)`. | ||
|
||
Both `print()` and `View()` are some of the many built-in functions[^1] available in R. | ||
|
||
[^1]: Functions are a collection of statements (organized and reusable code) that perform a specific task, and R has many built-in functions. | ||
|
||
In RStudio, the list of variables that have been loaded can be viewed in the environment pane. | ||
|
||
{fig-align="center" width="90%"} | ||
|
||
A screenshot of the environment pane with the stored variables. | ||
|
||
```{r} | ||
print(variable_1) | ||
``` | ||
|
||
```{r} | ||
View(variable_2) | ||
``` | ||
|
||
Output of `View()` will be seen in the script pane | ||
|
||
## The `assign()` and `rm()` functions | ||
|
||
In addition to using the assignment operators (`<-` and `=`), we can use the `assign()` function to assign a value to a variable. | ||
|
||
```{r} | ||
assign("variable_6", 555) | ||
variable_6 | ||
``` | ||
|
||
To remove the assignment of the value to the variable, either delete the variable in the "environment pane" or use the `rm()` function. | ||
|
||
```{r} | ||
variable_7 <- 159 | ||
``` | ||
|
||
```{r} | ||
rm(variable_7) | ||
``` | ||
|
||
After running `rm()` look at the environment pane to confirm whether `variable_7` has been removed. | ||
|
||
## Naming variables | ||
|
||
At this point, you may be wondering what conventions are used for naming variables. First, variables need to have meaningful names such as current_temp, time_24_hr, or weight_lbs. However, we need to be mindful of the [variable](https://web.stanford.edu/class/cs109l/unrestricted/resources/google-style.html) [style guide](http://adv-r.had.co.nz/Style.html) which provides us with the appropriate rules for naming variables. | ||
|
||
Some rules to keep in mind are: | ||
|
||
1. R is case-sensitive (`variable` is not the same as `Variable`), | ||
2. Names similar to typical outputs or functions (`TRUE`, `FALSE`, `if`, or `else`) cannot be used, | ||
3. Appropriate variable names can contain letters, numbers, dots, and underscores. However, you cannot start with an underscore, number, or dot followed by a number. | ||
|
||
**Examples of valid and invalid names** | ||
|
||
Valid names = time_24_hr / .time24_hr | ||
|
||
Invalid names = \_24_hr.time / 24_hr_time / .24_hr_time | ||
|
||
## Summary | ||
|
||
Overall, variables are very useful for programming in R/RStudio. These variables need to be named appropriately according to accepted conventions. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.