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
Please submit all assignments on Canvas in .doc, .docx, or .pdf file!
Click the Submit Assignment button and then upload the file
RStudio: working directory
R Code basics
R Script
Basic operations in R
## [1] 4.5
mean() function to do calculation and
give you a valid output## Error in Mean(1:8): could not find function "Mean"
Mean() does not exist
in R## Error: <text>:1:5: unexpected symbol
## 1: mea n
##         ^
mea n(), because in R no
function can have space in it.You also have to always have equal number of open and close parentheses
## 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.
To resolve this issue you can either
Add the missing parentheses
Click Esc to terminate the running of a function
How can RStudio know the location of the file?
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:
## [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!
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:
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.
Two ways to open a file in RStudio:
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
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("/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
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:
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
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 | 
For example, to calculate \(34*94*\frac{3^4}{5} - 7*(5^7)\) in R, type:
## [1] -495099.8
Calculate the following in R:
\(53*\frac{6}{2^4}\)
\(14*(30-3^3)\)
\(\frac{7}{9}*68-73*\frac{5}{2}\)
Calculate the following in R:
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…
To create an object, just type in the name and assign its value:
<- is an assigning operator in R
## [1] 13
## [1] "Katya"
## [1] 1 2 3
## [[1]]
## [1] "Katya"
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] "S371"
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!
Also you can now refer to the value using R object, so instead of typing:
## [1] 52
you can just type:
## [1] 52
The R object name cannot contain space:
## Error: <text>:1:7: unexpected symbol
## 1: katya baldina
##           ^
Reminder: all R objects will be gone once you close RStudio
If you assign another number to the same R object, the original number would be replaced:
## [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
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.