STAT311W24Week2

pdf

School

University of Washington *

*We aren’t endorsed by this school

Course

311

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by PrivateIron365

Report
Week 2: Familiarizing with RStudio & RMarkdown Dasha Petrov 2024-01-11 Adapted from Alfredo Effendy (another TA). Introduction There are three main parts of RMarkdown. 1. Header (YAML): To put title, author, date, and output type of the document. 2. Text Editor (White Space): Writing in text or LaTeX format, separate the different sections of the document, make comments about your code, analyzing the output of the codes, etc. If you want to put a comment in the grey space, we use . For example, 3. Code Editor (Grey Space): This is where you mainly code. You can create a code chunk with the following keyboard shortcuts: Mac: Cmd + Option + i Windows: Ctrl + Alt + i 2 + 2 ## [1] 4 2 + 1 ## [1] 3 If you want to put a comment in the grey space, we use hash (‘#’). Any character that is typed after ‘#’ will not be executed as a code. Basics 1. Function and library documentation in RStudio We use ‘?’ to get the documentation of function, library, and preloaded dataset. 1
# cars is pre-loaded dataset # this is the function we used to install library last time #install the package "tidyverse" for homework 1 # library that we installed last time to run RMarkdown 2. Differences between ‘=’ and ‘<-’ In R, ‘=’ is mainly used for function arguments, while ‘<-’ is dedicated for assignments. # I want to assign a value of 3 into variable a a <- 3 #read about the function that computes the mean #find the median of the dataset {3} 3. Writing Mathematical expressions in RMarkdown Familiarize yourself with the LaTeX syntax in order to write a good mathematical expression in RMarkdown. This is very important to know, because throughout the quarter you will encounter a lot of mathematical symbols, and if you do not use the LaTeX form, you will likely run into problems when knitting the document. Here is a basic guidance for writing mathematical symbols in RMarkdown: https://rpruim.github.io/s341/S19/from-class/MathinRmd.html Example: Writing fraction 5/7 In-line LaTeX In this study, we found out that . Note the single set of dollar signs. Equation Note the double dollar signs. 4. Debugging Tips A common mistake: Extra white space at the beginning and end of equations. will throw an error. It is in a comment so that the file will still knit. To fix this error, you need to write y = 5 . Note, the absence of extra white space at the beginning and end of the equation. If you are trying to knit your document and you are getting an error, but the error message is confusing: 1. Multiline comment your code and latex chunks sections at a time. Multiline comment by highlighting the text/latex/code with your cursor and using the keyboard shortcuts mentioned previously in the introduction section. 2
2. Knit your file 3. Repeat steps 1 and 2 until you successfully knit your file 4. The last section of code/latex that you commented has the error. Uncomment this section by high- lighting it again with your cursor and using the keyboard shortcut for commenting. Find the error, fix it, and make sure your document knits. 5. Uncomment the rest of the chunks you commented, and make sure the document still knits. Setting your working directory In order to successfully load the data, we need to make sure that the working directory is in the folder where the RMarkdown and dataset are located. If you get an error like “cannot open the connection”, “cannot open file”, or “No such file or directory”, you are likely in the wrong directory (or you have a typo). I like to store the file I’m working on and the dataset in the same location on my computer. Then, I can click Session -> Set Working Directory -> To Source File Location. If you prefer to store your file and the dataset in different locations on your computer, you can do the following: Session -> Set Working Directory -> Choose Directory. . . and select the folder the data is in on your computer. You can also do this with code. Supppose the data is located in my downloads folder. I would run the following code in the console, NOT in an rmarkdown code chunk: #check current working directory #set working directory You will need to change the path to the file accordingly. How to load an Excel file into RStudio One way to do this is with the package “readxl”. #install.packages("readxl") #?read_excel # If the RMarkdown file and dataset are in the same working directory/folder or you set the working dire How to load a CSV file into RStudio #?read.csv # If the RMarkdown file and dataset are in the same working directory/folder or you set the working dire # If the RMarkdown file and dataset are not in the same folder or you did not set the working directory 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
View the data Extracting information from the dataset We are interested in analyzing a specific variable in the dataset, GPA. # We can save the GPA column from dataset focus into a new variable # Get the average GPA of the students # Get the IQR GPA of the students Let’s now do some plotting of the variables # Plotting ## Histogram: To get the distribution of the data ## ?hist for information about possible parameters/arguments ## Boxplot: To get the distribution in addition to detect outlier as well ## ?boxplot for information about possible parameters/arguments ## Scatterplot: To get relationship between variables ## ?plot for information about possible parameters/arguments 4