PSCI 107 - Introduction to R

Recitation 1

Rithika Kumar

Sep 6, 2019

Goal for today

  1. Introductions
  2. Going over the syllabus
  3. Getting familiar with R studio and R Markdown
  4. Questions on logistics

Introduction

Introduction

Talk to your partner and introduce them!

Knowing the syllabus

Syllabus - your best friend

  1. What is my name?
  2. When are my OH? And where?
  3. How many problem sets do we have?
  4. IS the mid-term take home or in class?
  5. What is our policy on late submissions?
  6. Do we have a slack channel? Have you or your partner joined it?
  7. T or F: Collaborating on PS is allowed
  8. T or F: Making joint submissions if you worked on the PS together is allowed?
  9. T or F: Attendance during recitation will not count towards your grade

Timeline

Timeline - Important Dates

Timeline - Important Dates

R and R Studio

Getting started with R and R studio

R scrrenshot

R scrrenshot

Setting your Working directory

  1. Setting your working directory - Type the following in your script
  2. Open a new R Script (click on the button on the top left corner) and type the following
getwd()
setwd() 
#insert the path of the directory of your choice
  1. Click on RUN on the top right corner of the script
  2. Save your script. ALWAYS.
  3. This can also be done manually: Session–> setwd –> Choose directory

Running your code

  1. Rather than manually pressing run each time, use command + enter at the end of the code line

  2. “#” can be use in front of a line if you want to comment on your code

# you should use ## othis often!!! 

Libraries/Packages

R can do many statistical and data analyses. They are organized in so-called packages or libraries

You already installed one yesterday! (Remember?)

  1. To find out what packages you already have click on packages in the bottom right pane

  2. Check if you have , ,

  3. If you don’t then type the followin on your script

install.packages("rmarkdown")
install.packages("knitr")
install.packages("tinytex")
tinytex::install_tinytex()
  1. Once you have downloaded it, you need to call the package. Everytime you want to do

Objects in R

  1. R stores our data, graphs, computations etc in what we call R objects.
  2. It is always important to define your object first (using “<-”). For instance:
add <- 2+2
add #to display the answer of this calculation
  1. Once you define it, if you intend to call it later all you need to do is type out the name of the object

Working with R Markdown

You will be using R Markdown to submit your problem sets so let’s get familiar with it

  1. Open a new script within R Markdown - follow me
  2. Objective of using R Markdown – it lets you retain your code within the body of the text.
  3. Name the file
  4. Code chunks, running individual codes, knitting to pdf and saving

Exercise

Complete the following exercise in a new R Mardown file and knit it to a PDF

  1. calculate 2*4 and define it
  2. Knit this to pdf
  3. Remember to retain the code

Questions?

Thanks!