[1] 5
[1] 6
[1] 42
[1] 5
STAT 204 โ Introduction to Statistical Data Analysis
01 Sep 2025
R programming
stats
data visualization
data science
open and reproducible science
You can also find it on Canvas
Questions?
By the end of week 1, you will be able to:
Whatโs your experience with R?
A. Never heard of it
B. Heard of it, never used it
C. Used it a few times
D. Regular user
What do you hope to learn?
A. Data analysis
B. Statistical modeling
C. Data visualization
D. All of the above!
Turn to your neighbor and discuss your answers! ๐ฅ
Origins: R evolved from the S programming language developed at Bell Labs in the 1970s ๐ฌ
Creators: Ross Ihaka and Robert Gentleman at University of Auckland, New Zealand (1993) ๐ณ๐ฟ
Name: โRโ comes from the first names of its creators (Ross and Robert) ๐
Open Source: Made freely available in 1995, managed by R Core Team ๐
Current Status: Maintained by R Foundation for Statistical Computing ๐๏ธ
Advantages โ
Disadvantages โ
Note
Bottom line ๐ก R excels at statistical analysis and data visualization, but requires patience to master!
Four important panes (for now):
Source (center): Write and edit scripts โ๏ธ
Console (bottom): Execute commands โก
Session (top-right): See your data and objects, plots ๐
Explorer (left): Navigate and view files ๐
Hands-on exploration (5 minutes) โฑ๏ธ
Discuss with your neighbor: Compare answers, did you get the same ones?
Check out these slides from Posit: https://github.com/posit-dev/positron-workshop/tree/main/slides
Letโs start simple - R can do math!
Try it yourself! ๐งฎ
Calculate: Whatโs 23 ร 45? Whatโs the square root of 144?
Variables let us store values and reuse them:
[1] 15
[1] 50
X and x are different!) โ ๏ธBest practice ๐ก
Use descriptive names! student_age is much better than x1.
Your turn! (5 minutes) โฑ๏ธ
birth_year with your birth yearcurrent_year with 2025current_year - birth_yearmy_ageBonus: Create variables for your favorite number and color! ๐
Share with a neighbor: What variables did you create? ๐ฅ
R has several basic data types:
[1] "numeric"
[1] "character"
[1] "logical"
R has several basic object classes:
[1] "character"
[1] "numeric"
[1] "integer"
[1] "logical"
[1] "complex"
[1] "factor"
[1] "A" "B" "C"
[1] "factor"
[1] "numeric"
[1] "25"
[1] 1
Automatic conversion ๐
R will automatically convert (coerce) data types when needed, but be careful - this can cause unexpected behavior!
[1] "age" "complex_num" "count" "favorite_color"
[5] "fg" "final.grade" "grades" "height"
[9] "is_fun" "is_student" "likes_r" "my_name"
[13] "name" "student_age" "test_score_1" "ts1"
[17] "x" "x1" "y"
[1] TRUE
character(0)
tibble [344 ร 8] (S3: tbl_df/tbl/data.frame)
$ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
$ island : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
$ bill_length_mm : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
$ bill_depth_mm : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
$ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
$ body_mass_g : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
$ sex : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
$ year : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...
species island bill_length_mm bill_depth_mm
Adelie :152 Biscoe :168 Min. :32.10 Min. :13.10
Chinstrap: 68 Dream :124 1st Qu.:39.23 1st Qu.:15.60
Gentoo :124 Torgersen: 52 Median :44.45 Median :17.30
Mean :43.92 Mean :17.15
3rd Qu.:48.50 3rd Qu.:18.70
Max. :59.60 Max. :21.50
NA's :2 NA's :2
flipper_length_mm body_mass_g sex year
Min. :172.0 Min. :2700 female:165 Min. :2007
1st Qu.:190.0 1st Qu.:3550 male :168 1st Qu.:2007
Median :197.0 Median :4050 NA's : 11 Median :2008
Mean :200.9 Mean :4202 Mean :2008
3rd Qu.:213.0 3rd Qu.:4750 3rd Qu.:2009
Max. :231.0 Max. :6300 Max. :2009
NA's :2 NA's :2
Vectors store multiple values of the same type:
[1] 20 21 19 22 20
[1] "Alice" "Bob" "Charlie" "Diana"
[1] 20
[1] 19
[1] "Bob"
[1] 20 19
[1] 20 21 19
[1] 1 2 3 4 5 6 7 8 9 10
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
[1] 5 5 5
[1] 1 2 1 2 1 2
[1] 1 1 1 2 2 2
[1] 0 0 0 0 0
[1] "" "" ""
[1] NA
[1] TRUE
[1] NaN
[1] TRUE
[1] Inf
[1] -Inf
[1] TRUE
[1] FALSE FALSE TRUE FALSE TRUE FALSE
[1] TRUE TRUE FALSE TRUE FALSE FALSE
[1] "1" "2" "three" "4"
[1] "character"
[1] "TRUE" "1" "2.5" "text"
[1] 1.0 1.0 2.5
[1] 1 1
[1] 11 22 33 44
[1] 10 40 90 160
[1] 1 4 9 16
[1] 11 22 13 24
[1] 8
[1] 1 1 2 3 4 5 6 9
[1] 9 6 5 4 3 2 1 1
[1] 4.0 1.5 5.0 1.5 6.0 8.0 3.0 7.0
[1] 2 4 7 1 3 5 8 6
[1] 6 2 9 5 1 4 1 3
[1] 3 1 4 5 9 2 6
Bob Dave Grace
92 96 91
Alice Bob Eve Grace
85 92 88 91
Bob Dave Grace
2 4 7
Dave
4
Frank
6
Bob Carol Dave Eve Frank Grace
92 78 96 88 74 91
Bob Dave Eve Frank Grace
92 96 88 74 91
[1] TRUE
[1] FALSE FALSE FALSE TRUE TRUE
[1] 1 2 3 4 5 6 7 8
[1] 4 5
[1] 1 2 3
[1] FALSE
Practice time! (7 minutes) โฑ๏ธ
test_scores with these values: 85, 92, 78, 96, 88mean()max()student_names with 5 names of your choiceChallenge: Can you find the lowest score and which position itโs in? ๐
Matrices are 2-dimensional arrays that store data of the same type:
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[,1] [,2] [,3]
vec1 1 2 3
vec2 4 5 6
vec3 7 8 9
vec1 vec2 vec3
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[1] 3 4
[1] 3
[1] 4
[1] 12
Col1 Col2 Col3 Col4
Row1 1 4 7 10
Row2 2 5 8 11
Row3 3 6 9 12
[1] "Row1" "Row2" "Row3"
[1] "Col1" "Col2" "Col3" "Col4"
[[1]]
[1] "Row1" "Row2" "Row3"
[[2]]
[1] "Col1" "Col2" "Col3" "Col4"
[1] 4
Col1 Col2 Col3 Col4
2 5 8 11
Row1 Row2 Row3
7 8 9
Col2 Col3 Col4
Row1 4 7 10
Row2 5 8 11
[1] 4
Col1 Col2 Col3 Col4
Row1 1 4 7 10
Row3 3 6 9 12
Math Science History English
Student1 85 96 91 94
Student2 92 88 89 82
Student3 78 74 76 90
Student1 Student2 Student3
91.50 87.75 79.50
Math Science History English
85.00000 86.00000 85.33333 88.66667
Student1 Student2 Student3
96 92 90
Math Science History English
78 74 76 82
[,1] [,2]
[1,] 2 3
[2,] 1 4
[,1] [,2]
[1,] 1 2
[2,] 2 1
[,1] [,2]
[1,] 3 5
[2,] 3 5
[,1] [,2]
[1,] 1 1
[2,] -1 3
[,1] [,2]
[1,] 2 6
[2,] 2 4
[,1] [,2]
[1,] 8 7
[2,] 9 6
[,1] [,2]
[1,] 2 1
[2,] 3 4
[1] 5
[,1] [,2]
[1,] 0.8 -0.6
[2,] -0.2 0.4
eigen() decomposition
$values
[1] 5 1
$vectors
[,1] [,2]
[1,] -0.7071068 -0.9486833
[2,] -0.7071068 0.3162278
[,1] [,2]
[1,] 1 -1.110223e-16
[2,] 0 1.000000e+00
The sweep() function sweeps out a statistic from a matrix:
Subtract column means:
Each column now has mean = 0!
R supports higher-order arrays:
## , , 1
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
##
## , , 2
## [,1] [,2]
## [1,] 9 13
## [2,] 10 14
## [3,] 11 15
## [4,] 12 16
A list can contain different types of elements:
Using [] returns a list:
Part A: Lists
[[]]Trimming strings:
Data frames track variable types automatically!
Matrix-style indexing:
R provides functions for standard distributions:
Part A: Data Frames
Part B: Random Numbers
Binomial distribution:
# Multiple ways to get help
?mean # Help for specific function
help(mean) # Same as above
??regression # Search for topic
help.search("regression") # Same as ??
# See function examples
example(mean)
example(plot)
# Get help for packages
help(package = "tidyverse")
# See all functions in a package
ls("package:base")Best Practice ๐
Use <- for assignment. Itโs the R convention and makes your code more readable!
Keyboard shortcut: Alt + - (Windows) or Option + - (Mac)
[1] 5
[1] 10
[1] 2
Error in Mean(c(1, 2, 3)): could not find function "Mean"
Error: object 'Data' not found
[1] 10
Error: object 'Penguins' not found
# A tibble: 6 ร 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen NA NA NA NA
5 Adelie Torgersen 36.7 19.3 193 3450
6 Adelie Torgersen 39.3 20.6 190 3650
# โน 2 more variables: sex <fct>, year <int>
maen() instead of mean() โ
mean instead of mean() โ
() โ
name <- Alice instead of name <- "Alice" โ
Mean() instead of mean() โ
Tip
Donโt worry about making mistakes - theyโre part of learning! Read error messages carefully. ๐ง
Error: object 'Ages' not found
Reading error messages ๐
Your mission (15 minutes) ๐ฏ
Create a simple analysis of your class!
student_names, birth_months, favorite_numbers. Assign 50 random numbers to months (from 1 to 12), 50 random numbers from 0 to 100 in the favorite numbers column, and use the package randomNames to generate 50 names. Create a data frame with all three columns.Bonus: Try to find out which birth month is most common using table() ๐๏ธ
Work in pairs and help each other! ๐ฅ
Quarto has built-in support for LaTeX typesetting:
Quarto combines code, text, and output in one document:
# My Analysis
::: {.cell}
```{.r .cell-code}
# This code will run and show results
summary(penguins)
```
::: {.cell-output .cell-output-stdout}
```
species island bill_length_mm bill_depth_mm
Adelie :152 Biscoe :168 Min. :32.10 Min. :13.10
Chinstrap: 68 Dream :124 1st Qu.:39.23 1st Qu.:15.60
Gentoo :124 Torgersen: 52 Median :44.45 Median :17.30
Mean :43.92 Mean :17.15
3rd Qu.:48.50 3rd Qu.:18.70
Max. :59.60 Max. :21.50
NA's :2 NA's :2
flipper_length_mm body_mass_g sex year
Min. :172.0 Min. :2700 female:165 Min. :2007
1st Qu.:190.0 1st Qu.:3550 male :168 1st Qu.:2007
Median :197.0 Median :4050 NA's : 11 Median :2008
Mean :200.9 Mean :4202 Mean :2008
3rd Qu.:213.0 3rd Qu.:4750 3rd Qu.:2009
Max. :231.0 Max. :6300 Max. :2009
NA's :2 NA's :2
```
:::
:::
The mean bill length is 43.9219298 mm.โ
Positron Interface: Four panes and their purposes ๐ฅ๏ธ
โ
R as Calculator: Basic and advanced operations ๐งฎ
โ
Variables: Storing and reusing information ๐
โ
Data Types: Numeric, character, logical ๐ข๐โ
โ
Vectors: Collections of data ๐
โ
Functions: Getting help and using built-in functions ๐ ๏ธ
โ
Packages: Extending Rโs capabilities ๐ฆ
โ
Scripts & Quarto: Organizing your work ๐
Next class weโll cover:
Think-pair-share (5 minutes) โฑ๏ธ
Think (1 min): Name one thing you learned about R that you didnโt know before.
Pair (2 min): Share with your neighbor and listen to their surprise ๐ฅ
Share (2 min): Share it on Ed Discussion ๐ฃ๏ธ
Remember: Every expert was once a beginner! ๐ฑ
Office Hours: Tu and Th after lecture ๐
Email: macordob@ucsc.edu ๐ง
Course Website: Course URL ๐
Next class: More data wrangling and visualization ๐
Keep exploring and have fun with R! ๐
![]()
STAT 204 โ Intro to Statistical Data Analysis
Comments and good habits
Best practice ๐
Always comment your code! Explain why youโre doing something, not just what youโre doing.