Notes for dapR1 Live R Session; Week 1, Semester 1
Notes for dapR1 Live R Session; Week 1, Semester 1
Week 1 Summary of the Session
Overview of Session: Basic features of RStudio and reading data files.
Access RStudio: Via the link on the Learn page in the “Quick Links” section.
Click on “RStudio Server Online (by Notable)” and select “RStudio” for the notebook server.
Purpose of Notes: Provided to help you remember main points shown in real-time during the session.
Using R as a Calculator
R can operate in interactive mode, functioning as a calculator.
Console Panel: Located on the left in RStudio, often requires selection.
Input commands after the ‘>’ prompt, followed by pressing Return.
Basic Calculations
Example command:
2*8Output:
## [1] 16More complex calculation:
2*(8+3)Output:
## [1] 22
Storing Results in Variables
Assigning Values: Use the assignment operator “<-” to store results.
Example:
mynumber <- 2*(8+3)
Accessing Variables: Type name in console to see its value.
mynumberOutput:
## [1] 22
Using Variables in Calculations:
mynumber/2Output:
## [1] 11
Loading Packages and Reading in Data
Special functions often require loading packages in R.
Packages Panel: Located in the bottom right of RStudio, shows a list of packages.
Loading Tidyverse Package:
Command:
library(tidyverse)
Reading Data: Execute a command to read a dataset used in the lecture.
Command:
mydata <- read_csv("https://uoepsy.github.io/data/lecture1_data.csv")Uses function
read_csv()with URL in quotes.
Examining the Data
Displaying the Data: Can simply type the variable name.
Command:
mydataOutput:
## # A tibble: 5 x 9 ## ID Hair_colour Likert_item Likert_values Degree ReactionTime Height_cm ## <chr> <chr> <chr> <dbl> <chr> <dbl> <dbl> ## 1 ID101 Brown Strongly Agree 5 No 1.2 191. ## 2 ID102 Brown Agree 4 No 0.9 181. ## 3 ID103 Blonde Agree 4 Yes 3.2 165. ## 4 ID104 Blonde Disagree 2 Yes 55.5 177. ## 5 ID105 Black Strongly Disagr~ 1 Yes 2.1 201 ## # i 2 more variables: Weight_kg <dbl>, IQ <dbl>Understanding Output Format:
Data shown as a “tibble”, with 5 rows and 9 columns.
Each column has variable names and types (e.g., “chr” = character, “dbl” = double).
Using Structure and Glimpse Commands
Structure Command: Gives a detailed structure of the dataset.
Command:
str(mydata)Output:
## spc_tbl_ [5 x 9] (S3: spec_tbl_df/tbl_df/tbl/data.frame) ## $ ID : chr [1:5] "ID101" "ID102" "ID103" "ID104" ... ## $ Hair_colour : chr [1:5] "Brown" "Brown" "Blonde" "Blonde" ... ## $ Likert_item : chr [1:5] "Strongly Agree" "Agree" "Agree" "Disagree" ... ## $ Likert_values: num [1:5] 5 4 4 2 1 ## $ Degree : chr [1:5] "No" "No" "Yes" "Yes" ... ## $ ReactionTime : num [1:5] 1.2 0.9 3.2 55.5 2.1 ## $ Height_cm : num [1:5] 191 181 165 177 201 ## $ Weight_kg : num [1:5] 88.9 76.6 52 81.5 105.8 ## $ IQ : num [1:5] 100 105 99 120 131Glimpse Command: Provides a concise overview.
Command:
glimpse(mydata)Output:
## Rows: 5 ## Columns: 9 ## $ ID <chr> "ID101", "ID102", "ID103", "ID104", "ID105" ## $ Hair_colour <chr> "Brown", "Brown", "Blonde", "Blonde", "Black" ## $ Likert_item <chr> "Strongly Agree", "Agree", "Agree", "Disagree", "Strongl~ ## $ Likert_values <dbl> 5, 4, 4, 2, 1 ## $ Degree <chr> "No", "No", "Yes", "Yes", "Yes" ## $ ReactionTime <dbl> 1.2, 0.9, 3.2, 55.5, 2.1 ## $ Height_cm <dbl> 191.2, 180.8, 165.3, 177.1, 201.0 ## $ Weight_kg <dbl> 88.9, 76.6, 52.0, 81.5, 105.8 ## $ IQ <dbl> 100, 105, 99, 120, 131
Accessing Specific Data Points
Accessing a Specific Column: Use dollar sign operator.
Command:
mydata$Hair_colourOutput:
## [1] "Brown" "Brown" "Blonde" "Blonde" "Black"
Accessing Specific Cells:
Example: Row 3, Column 4:
Command:
mydata[3,4]Output:
## # A tibble: 1 x 1 ## Likert_values 3Whole Row Access:
Command:
mydata[3,]Whole Column Access:
Command:
mydata[,4]
Dimensions of the Data
Number of Rows:
Command:
nrow(mydata)Output:
## [1] 5
Number of Columns:
Command:
ncol(mydata)Output:
## [1] 9
Overall Dimensions:
Command:
dim(mydata)Output:
## [1] 5 9
Adding Data to the Dataset
Adding a New Column: Assign values to a new column using the dollar-sign operator.
Command:
mydata$fav_animal <- c("cat","dog","horse","rabbit","donkey")Viewing the Updated Dataset:
Command:
mydata
Using
c()Function: Used to create a list in R.
Declaring a Factor
A factor is a variable interpreted as a set of categories (levels).
Example: The “Degree” variable could be a factor with levels “Yes” and “No”.
Declaring Factor:
Command:
mydata$Degree <- factor(mydata$Degree)
Confirming Factor Status: Run the
str()command.
Uploading, Creating, and Exporting Files
Upload Files: Click the “Upload” button in the Files panel to upload files from your computer.
Saving a Dataset as CSV:
Command:
write_csv(mydata, file="mydata.csv")The file will appear in the “Files” tab.
Exporting Files to Local Disk:
Select file checkbox, click “More” and choose “Export”.
Creating and Using R Markdown Documents
Purpose of R Markdown: To save work and include R code chunks that can run within the document.
Creating R Markdown Document:
Through:
File > New File > R MarkdownThis creates a new file with example contents that can be edited.