Practical Data Manipulation Techniques
Practical Data Manipulation Techniques
Welcome to Practical Data Manipulation Techniques! In this unit, we’ll combine and build upon everything you've learned about data transformation in Ruby. You’ll work through techniques for filtering, projecting, and aggregating data, using methods like map, select, sum, and reduce. By the end, you’ll know how to harness these methods to analyze and summarize data effectively.
Let’s dive in!
Setting Up Our Dataset
Throughout this unit, we’ll work with a structured dataset to apply and combine the techniques you’ve learned. Here’s an array of hashes representing individuals with different attributes:
This dataset will be the foundation as we explore data manipulation techniques.
Selecting Specific Fields (Data Projection)
Data projection is used to select specific fields from each entry in a dataset. Let’s say we only want to see each person's name and profession:
In this example:
mapiterates through each person in the dataset.selectextracts only thenameandprofessionfields.
The result is an array of hashes containing only the projected fields.
Filtering Data Based on Conditions
Filtering allows you to keep only the data that matches specific conditions. Let’s select only the individuals who are 30 years or older:
Here:
selectfilters entries where theageis 30 or above.- The result contains only entries matching this age criterion.
