A matrix is a 2-dimensional structure where every element is the same data type (like a vector, but with rows and columns).
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
## [1] 3 4
## [1] 3
## [1] 4
[row, column]## [1] 8
## [1] 1 4 7 10
## [1] 4 5 6
## [,1] [,2]
## [1,] 7 10
## [2,] 8 11
## [,1] [,2]
## [1,] 6 10
## [2,] 8 12
## [,1] [,2]
## [1,] 5 21
## [2,] 12 32
## [,1] [,2]
## [1,] 23 31
## [2,] 34 46
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
A list can hold different data types, even other lists, vectors, or data frames — extremely flexible.
student <- list(
name = "Aisha Ali",
age = 22,
scores = c(88, 91, 76),
is_graduating = TRUE
)
student## $name
## [1] "Aisha Ali"
##
## $age
## [1] 22
##
## $scores
## [1] 88 91 76
##
## $is_graduating
## [1] TRUE
## [1] "Aisha Ali"
## [1] 88 91 76
## [1] 88 91 76
## $age
## [1] 22
## [1] 22
Key distinction:
[ ]always returns the same type of object (a smaller list);[[ ]]extracts the actual element/value inside.
A data frame is the structure you will use constantly in real data analysis: a table where each column is a vector (all one type), but different columns can hold different types — just like a spreadsheet.
students_df <- data.frame(
name = c("Amina", "Hodan", "Yusuf", "Deka"),
age = c(21, 23, 19, 22),
score = c(88, 92, 65, 79),
passed = c(TRUE, TRUE, TRUE, TRUE)
)
students_df## name age score passed
## 1 Amina 21 88 TRUE
## 2 Hodan 23 92 TRUE
## 3 Yusuf 19 65 TRUE
## 4 Deka 22 79 TRUE
## 'data.frame': 4 obs. of 4 variables:
## $ name : chr "Amina" "Hodan" "Yusuf" "Deka"
## $ age : num 21 23 19 22
## $ score : num 88 92 65 79
## $ passed: logi TRUE TRUE TRUE TRUE
## name age score passed
## Length:4 Min. :19.00 Min. :65.0 Mode:logical
## Class :character 1st Qu.:20.50 1st Qu.:75.5 TRUE:4
## Mode :character Median :21.50 Median :83.5
## Mean :21.25 Mean :81.0
## 3rd Qu.:22.25 3rd Qu.:89.0
## Max. :23.00 Max. :92.0
## [1] 4
## [1] 4
## [1] "name" "age" "score" "passed"
## name age score passed
## 1 Amina 21 88 TRUE
## 2 Hodan 23 92 TRUE
## [1] "Amina" "Hodan" "Yusuf" "Deka"
## [1] 88 92 65 79
## name age score passed
## 1 Amina 21 88 TRUE
## name age score passed
## 1 Amina 21 88 TRUE
## 2 Hodan 23 92 TRUE
## [1] "Amina" "Hodan"
A factor stores categorical variables (like “Male”/“Female” or “Freshman”/“Sophomore”/“Junior”/“Senior”) with a fixed, known set of possible values called levels.
year_level <- factor(c("Junior", "Freshman", "Senior", "Sophomore"),
levels = c("Freshman", "Sophomore", "Junior", "Senior"))
year_level## [1] Junior Freshman Senior Sophomore
## Levels: Freshman Sophomore Junior Senior
## [1] "Freshman" "Sophomore" "Junior" "Senior"
## [1] 3 1 4 2
Scenario: Building a small student registry from scratch.
registry <- data.frame(
student_id = 1:5,
name = c("Ali", "Sara", "Deka", "Omar", "Layla"),
major = factor(c("ICT", "Business", "ICT", "Medicine", "Business")),
gpa = c(3.4, 3.8, 2.9, 3.6, 3.1)
)
str(registry)## 'data.frame': 5 obs. of 4 variables:
## $ student_id: int 1 2 3 4 5
## $ name : chr "Ali" "Sara" "Deka" "Omar" ...
## $ major : Factor w/ 3 levels "Business","ICT",..: 2 1 2 3 1
## $ gpa : num 3.4 3.8 2.9 3.6 3.1
## student_id name major gpa
## 1 1 Ali ICT 3.4
## 3 3 Deka ICT 2.9
# Average GPA by simply filtering
cs_avg_gpa <- mean(registry$gpa[registry$major == "ICT"])
cat("Average GPA of ICT students:", round(cs_avg_gpa, 2), "\n")## Average GPA of ICT students: 3.15
$
and [[ ]].title,
author, year, pages.is_long that is TRUE if
pages > 300.class_year with levels
"100 Level" through "400 Level", ordered.str() and summary() on your books data
frame and describe what each function tells you.Q1. What is the key difference between a matrix and a data frame?
Q2. Which operator returns the actual value stored inside a list element (not a sub-list)?
[ ][[ ]]( ){ }Q3. What R structure is best suited for a categorical variable like “Freshman/Sophomore/Junior/Senior”?
Q4. What does
students_df[students_df$score > 80, ] do?
Q5. What function shows the internal structure (types and preview) of a data frame?
summary()str()head()dim()Next Lesson: Importing and Exporting Data (CSV, Excel).