r notes

Author

Y. Rosenthal

Published

October 31, 2023

About This Website

This website is for the use of my students at YU.

The menu on the left lists the “chapters” in the “book”. When you click on a “chapter” from the left hand menu, you should see another menu appear on the right hand side of the screen. The menu on the right side contains links to sections in that chapter. It’s not perfect, but I’m working on improving the links.

One of the many nice things about this website is that the output of all R code is shown below the actual R code (see below for an example). The output is actually generated by R. Therefore, if there is a difference between my comments and the output shown below the code, then I must have made a typo in my comments (or in the code). You can trust the results displayed below the code more than the #comments that I wrote.

FYI - I created this website by using “Quarto” which is the new version of what has for years been called “R Markdown”. Quarto simplifies the publishing this type of content online, or in a PDF, or an ebook, etc.

-Prof. Rosenthal

Example - what R code in this book looks like

The following shows R code that displays the results of doing some basic arithmetic. The grey boxes contain R code. The output of each line of code is shown below the corresponding grey box. Each output from R below starts with [1] followed by the result of the calculation. For now, you can safely ignore the [1]. Later in the book we explain what the [1] means.

# Everything in the grey boxes is R code.
# The info that starts with a # is actually ignored by R and is known as a 
# comment. You can write whatever you like in a comment.

2+3           # add two plus three
[1] 5
2 ^ 3         # two to the power of three (ie. 2*2*2)
[1] 8
100 - 2 * 3   # R knows PEMDAS - answer is 94, not 296
[1] 94
(100 - 2) * 3 # now the answer is 296
[1] 294

The R “>” prompt symbol

Note that the appearance of the code in this book is a little different than what you would see when you actually use R. The R environment displays a > sign to indicate where to type a command. You don’t actually type in the initial “>” sign. This is simply R’s way of expressing to you that it is expecting you to type in a command. This “>” sign is known as R’s “prompt” symbol (i.e. it prompts you to enter a command). The above code would look as shown below when you actually type it into R. This book was created with the Quarto tool and by default Quarto does not show the “>” propmts.

> # Everything in the grey boxes is R code.
> # The info that starts with a # is actually ignored by R and is known as a 
> # comment. You can write whatever you like in a comment.
 
> 2+3           # add two plus three
[1] 5
 
> 2 ^ 3         # two to the power of three (ie. 2*2*2)
[1] 8
 
> 100 - 2 * 3   # R knows PEMDAS - answer is 94, not 296
[1] 94
 
> (100 - 2) * 3 # now the answer is 296
[1] 294