R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
11: Lists and Matrices
Think about the difference between a crate of soda and your kitchen junk drawer. A soda crate is perfectly uniform; every slot holds the exact same thing (a can), and they're arranged in a predictable grid of rows and columns. If I tell you to grab the can in the second row, third column, you know exactly what you're getting. That's a matrix.
Your junk drawer, on the other hand, is a chaotic mix. You've got a spare battery, a rubber band, a couple of old receipts, and maybe a small box that contains even more tiny things. It's not a grid; it's just a collection of "stuff" where each item can be completely different from the next. That's a list.
Handling Grids of Same-Type Data
In R, a matrix is basically a vector with dimensions. The catch is that everything in a matrix must be the same data type. If you try to sneak a string into a matrix of numbers, R will silently convert all those numbers into strings to keep the peace. I've seen this trip up plenty of developers who then wonder why their math operations are suddenly failing.
Let's say we're tracking the high temperatures for three cities over four days. We can represent this as a matrix:
# Creating a 3x4 matrix of temperatures
temp_data <- c(72, 75, 71, 78, 68, 70, 65, 67, 82, 85, 88, 84)
weather_matrix <- matrix(temp_data, nrow = 3, ncol = 4)
# Let's give the rows and columns some meaning
rownames(weather_matrix) <- c("New York", "Seattle", "Miami")
colnames(weather_matrix) <- c("Mon", "Tue", "Wed", "Thu")
print(weather_matrix)
To get a specific value, you use two indices: [row, column]. If I want Seattle's temperature on Wednesday, I'd use weather_matrix[2, 3]. Simple, right? It's just like coordinates on a map.
The Ultimate Catch-All: Lists
Now, matrices are great for math, but real-world data is rarely that tidy. Most of the time, you need to group together different types of information. This is where the list comes in. A list can hold a numeric vector, a character string, a matrix, and even another list—all inside one variable.
I usually use lists when I'm building a "container" for a specific object, like a user profile or a project configuration.
# Creating a list to represent a research subject
subject_profile <- list(
name = "Alice Smith",
age = 28,
test_scores = c(92, 88, 95),
is_active = TRUE,
metadata = matrix(1:4, nrow = 2)
)
Here, subject_profile isn't just a collection of values; it's a collection of different objects. You can access elements by their name using the $ operator, which is the way I do it 90% of the time because it makes the code much more readable: subject_profile$name gives you "Alice Smith".
The Double Bracket Headache
Here is where most people get stuck. In R, there's a massive difference between [ ] and [[ ]] when dealing with lists. This is a quirk that honestly takes a minute to click.
subject_profile[1]: This returns a list containing the first element. It's like taking the junk drawer and putting the battery inside a smaller box. You still have a "container."subject_profile[[1]]: This returns the actual element itself. This is like reaching into the drawer and grabbing the battery directly.
If you want to do something with the data—like add 5 to the age—you need the actual value, not a list containing the value. So, you'd use subject_profile[[2]] + 5 or, more simply, subject_profile$age + 5.
📋 Practical Task
Building a Sensor Station Metadata Store
You are tasked with organizing data for a remote weather station. You need to create a single R object that stores the station's identity and its raw readings.
Your Requirements:
- Create a matrix called
readingswith 2 rows (representing 'Temperature' and 'Humidity') and 3 columns (representing 'Hour 1', 'Hour 2', and 'Hour 3'). Fill it with any numeric values of your choice. - Create a list called
station_datathat contains:- A character string for the
station_id(e.g., "STATION_01"). - A logical value for
is_operational(set it toTRUE). - The
readingsmatrix you created earlier.
- A character string for the
- Using the
$operator, extract the humidity reading for 'Hour 2' from inside the list and print it to the console. (Hint: You'll need to chain the list access and the matrix index).
There are no comments for now.