Learn more about cleaning data with R: https://www.datacamp.com/courses/cleaning-data-in-r
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.
To understand the structure of your data, you have several tools at your disposal in R. Here, we read in a simple dataset 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's a data frame, 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 dataset with the dim() function. This particular dataset 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 7 columns has a name: year, avg_free, avg_reduced, and so on.
Okay, so we're starting to get a feel for things, but let's dig deeper. The str() (for "structure") function 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 dataset, 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 dataset happens to be entirely integers and numerics. We'll have a closer look at these datatypes 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, and the 25th, 50th, and 75th percent quartiles (also referred to as the first quartile, median, and 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 we can use the class() function to see the class of a dataset, the dim() function to view its dimensions, names() to see the column names, str() to view its structure, glimpse() to do the same in a slightly enhanced format, and summary() to see a helpful summary of each column.
Time to practice!