R tutorial: Exploring raw data

Learn how to explore raw data in R using the Lunch dataset, which tracks US school lunches from 1969 to 2014. Use class(), dim(), names(), str(), glimpse(), and summary() functions to understand your data's structure, dimensions, and values before analysis.
Chapters
The first step in the data cleaning process is exploring your raw data. We can think of data exploration itself as a three step process consisting of understanding the structure of your data, looking at your data, and visualizing your data. So to understand the structure of your data, you have several tools at your disposal in R Here we we read in a simple data set called Lunch, which contains information on the number of free, reduced price and full price school lunches served in the US from 1969 through 2014. First we check the class of the lunch object to verify that it is in fact a DataFrame or a two dimensional table consisting of rows and columns of which each column is a single data type, such as numeric character, etc. We then view the dimensions of the data set with the DIM function. This particular Data set has 46 rows and 7 columns. Dim always displays the number of rows first, followed by the number of columns. Next we take a look at the column names of Lunch with the names function. Each of the seven columns has a year average, free, average reduced, and so on. Okay, so we're starting to get a feel for things, but let's dig a little deeper. The STR function for structure is one of the most versatile and useful functions in the R language because it can be called on any object and will normally provide a useful and compact summary of its internal structure. When passed a data frame, as in this case, STR tells us how many rows and columns we have. Actually, the function refers to rows as observations and columns as variables, which strictly speaking is true in a tidy data set, but not always the case, as you'll see in the next chapter. In addition, you see the name of each column followed by its data type and a preview of the data contained in it. The Lunch data set happens to be entirely integers and numerics. We'll have a closer look at these data types in chapter 3. The dplyr package offers a slightly different flavor of STR called Glimpse, which offers the same information but attempts to preview as much of each column as will fit neatly on your screen. So here we first load dplyr with the library command, then call glimpse with a single argument Lunch Another extremely helpful function is Summary, which when applied to a data frame, provides a useful summary of each column. Since the LUNCH data are entirely integers and numerics, we see a summary of the distribution of each column, including the minimum and maximum, the mean, the 25th, 50th and 75th percent quartiles, also referred to as the first quartile, the median, and the third quartile, respectively. As you'll soon see, when faced with character or factor variables, summary will produce different summaries to review. You've seen how you can use the class function to see the class of a data set, the dim function to view its dimensions, names to see the column names, STR to view its structure, and glimpse to do the same, but in a slightly enhanced format and summary to see a helpful summary of each column. It's time to practice.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







