Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 readings with 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_data that contains:
    • A character string for the station_id (e.g., "STATION_01").
    • A logical value for is_operational (set it to TRUE).
    • The readings matrix you created earlier.
  • 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).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.