S371: Lab 2

Lab Instructor: Katya Baldina ()

2023-08-30

Announcement

All HW deadlines are moved from Wednesdays to Thursdays!!

HW0 is due tomorrow (Aug 31)! Please, don’t forget to submit it!

HW1 is due Sep. 7!!!

Lab Notes and R Codes are now here: https://katyalex.github.io/

You can also make an appointment with me here: https://calendar.app.google/SDGU3k2BU7jmmZsy6

Announcement

Please submit all assignments on Canvas in .doc, .docx, or .pdf file!

Announcement

Click the Submit Assignment button and then upload the file

Today

Three ways to use RStudio

https://uits.iu.edu/services/technology-for-teaching/instruction-and-assessment-tools/iuanyware/index.html

R Code

mean(1:8)
## [1] 4.5
Mean(1:8)
## Error in Mean(1:8): could not find function "Mean"

R Code

mea n(1:8)
## Error: <text>:1:5: unexpected symbol
## 1: mea n
##         ^

R Code

You also have to always have equal number of open and close parentheses

mean(1:8
     
## Error: <text>:3:0: unexpected end of input
## 1: mean(1:8
## 2:      
##   ^

R would not give you a result. Instead you will see the + sign, which will encourage you to end the code of line.

R Code

To resolve this issue you can either

  1. Add the missing parentheses

  2. Click Esc to terminate the running of a function

Types of R files

  1. R script file It has the .R extension (e.g. HW0.R) and contains R codes
  2. R data file It has the .Rdata, .RData, .Rda, or .rda extension (e.g. HW4.RData)

Loading scripts and files into R

How can RStudio know the location of the file?

  1. Click File –> Open File in RStudio
  2. Type R code to tell R to load files from the working directory into RStudio

Working directory is a folder that R will refer to when you ask it to load files. If the files that you ask to load are not in the working directory, R will give you an error.

You can change the location of the working directory either temporarily or permanently. I recommend to set up the working directory at the beginning of your code each time, because often you need to store files from different projects in different folders.

To check working directory, type:

getwd()
## [1] "/Users/KATE1/Library/CloudStorage/OneDrive-IndianaUniversity/Active/Fall 2023/S371_Lab/Lab"

To change working directory, type:

setwd('/Users/KATE1/Desktop')
getwd() #To check that working directory changed, run this command again!
## [1] "/Users/KATE1/Desktop"

Now you can see that your working directory has changed!

Changing Working Directory Manually

In RStudio, you can also change the location of the working directory going to Session –> Set Working Directory –> Choose Directory

Then look for the folder you want to set up as the working directory.

For the further guide, you can refer to this resource:

https://rstudio-education.github.io/hopr/dataio.html

Changing Working Directory Permanently

If you want to change the location of working directory permanently, you can do it in RStudio.

Just go to Tools–>Global Options

Click the browse button and change the default location of the working directory.

Running R script file

Two ways to open a file in RStudio:

  1. Look for the file by clicking File –> Open File in RStudio
  2. Type R code to load files from the working directory into RStudio

Let’s open R script file

  1. Go to your assignments on Canvas and open Problem Set 0
  2. Download HW0.R file
  3. In RStudio go to File –> Open File
  4. Then look for the HW0.R script file in the place, where you downloaded it

Let’s run R script file

Highlight the lines of code that you want to run:

Then press the run button at the top of your script

OR

Mac Users: Command+Enter

Windows Users: Ctrl+Enter

Comments

Anything that starts with # is a comment and ignored by R

Comments are useful to make notes about your code, so that you know what you are doing.

## Directions:
## This homework is intended simply to show us that you can run scripts in R.
## Run this script. It will print out a seven digit number and a time stamp.
## Copy the whole thing and submit it using Canvas online submission.
## There is no need to understand any what it does, but if you figure it out
## at any point in the semester, send me an email explaining it.

Another way to open R script

To open your R script you need to put it in your working directory OR include the path into the file name, to pull it up. Type in console the following code:

file.edit("HW 0.R")
file.edit("/Users/KATE1/Library/CloudStorage/OneDrive-IndianaUniversity/Active/Fall 2023/S371_Lab/Lab/HW 0.R")

Note: no output is supposed to be displayed after inputting this line

You always have to use quotes to pull up any file from outside of R

Let’s open R data file

Manually: In RStudio go to File –> Open File

If your file already in the working directory, run the following line of code in the console window or in your script:

load("medalsHW1.RData")

Or you can use the full path to file to access it:

load("/Users/KATE1/Library/CloudStorage/OneDrive-IndianaUniversity/Active/Fall 2023/S371_Lab/Lab/medalsHW1.RData")

Note: no output is supposed to be displayed after inputting this line

load() function lets you to load a R data file

After loading the R data file, you should see a new R object in the Environment window

Basic Operations in R

R can be used as a calculator

Just type the formula accordingly

Symbol on the Keyboard Operation
+ Summation
- Subtraction
* Multiplication
/ Division
^ Powers
 sqrt() or ^0.5 Square Root

Basic Operations in R

For example, to calculate \(34*94*\frac{3^4}{5} - 7*(5^7)\) in R, type:

34*94*(3^4)/5-7*(5^7)
## [1] -495099.8

Let’s practice!

Calculate the following in R:

  1. \(53*\frac{6}{2^4}\)

  2. \(14*(30-3^3)\)

  3. \(\frac{7}{9}*68-73*\frac{5}{2}\)

Let’s practice!

Calculate the following in R:

  1. \(53*\frac{6}{2^4}\)
53*(6/(2^4))
  1. \(14*(30-3^3)\)
14*(30-3^3)
  1. \(\frac{7}{9}*68-73*\frac{5}{2}\)
(7/9)*68-73*(5/2)

R object

R object: it is the thing that you can store in your current R working environment (note: all R objects will be gone once you close RStudio)

You can store almost anything in R as an object:

• A number

• Characters

• A vector (a list of numbers/a list of characters)

• Logicals (TRUE/FALSE)

• Data frame (kind of like an Excel spreadsheet with numbers stored in columns and rows)

• and many others…

R object

To create an object, just type in the name and assign its value:

<- is an assigning operator in R

x <- 13 #you can assign just one single value like number
x
## [1] 13
name <- "Katya" #or you can assign a character
name
## [1] "Katya"
y <- c(1, 2, 3) #you can also assign a vector of numbers
y
## [1] 1 2 3
lst1 <- list("Katya", 2, "S371") #or list of numbers and characters
lst1
## [[1]]
## [1] "Katya"
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] "S371"

R object

Let’s take a look at the Environment panel

You can see that all objects that I created are now stored in the Environment panel!

R object

Also you can now refer to the value using R object, so instead of typing:

13*4
## [1] 52

you can just type:

x <- 13
x*4
## [1] 52

R object

The R object name cannot contain space:

katya baldina <- 13
## Error: <text>:1:7: unexpected symbol
## 1: katya baldina
##           ^

Reminder: all R objects will be gone once you close RStudio

R object

If you assign another number to the same R object, the original number would be replaced:

x <- 13 #I first assign the number of 13 to an R object “x”
x
## [1] 13
x <-28 #Then I assign the number of 28 to an R object “x”
x #Now the R object “x” stores the number of 28 instead of 13
## [1] 28

Do you have any questions?