Why Use a Compact Column Scan?
A wide data frame can contain many fields: identifiers, dates, exposure measures, categorical indicators, and outcomes. Before choosing later analyses, an analyst needs a quick way to see what columns are present and how R currently represents them. The dplyr function glimpse() provides that scan in a compact, column-oriented display.
This is a structural inspection, not a complete analysis of the values. A glance at a few displayed entries can reveal an unexpected class or a suspicious-looking value, but it cannot establish a column’s full distribution, count its missing values, or prove that its entries are correct. Those questions require other checks.
Reading glimpse() Output
Load dplyr or call the function with its namespace, as in dplyr::glimpse(policy_data). The output is usually organized with one line per column. This orientation is useful when there are many columns: names and types can be scanned vertically, rather than trying to fit a very wide table across the console.
For example, suppose a hypothetical insurer has a data frame called policy_data. A shortened, illustrative display might begin with a row-and-column count, followed by lines resembling “$ policy_id <chr> P104, P105, ...”, “$ issue_year <int> 2022, 2023, ...”, and “$ premium <dbl> 480, 525, ...”. The exact formatting and how many values fit can depend on the R environment and package versions.
Read the display in three passes. First, note the stated dimensions: they orient you to the size of the object being inspected. Second, scan the names and types to check whether the fields you expect are present and plausibly represented. Third, examine the shown values for obvious surprises, while remembering that unseen rows may differ.
- Names identify the columns. A missing or misspelled expected name can matter before any model is built.
- Types show how R currently stores each column. For example, a numeric-looking value stored as character may need investigation before numeric calculations are appropriate.
- Example values give a limited visual preview. They can help spot an obvious issue, but they are not a summary of all observations.
Types are clues, not verdicts. A character column may be intentional, and a numeric column may still contain invalid values. In particular, a glimpse that displays a few ordinary values does not establish that every row is valid. Use the earlier material on R variable classes to interpret the class labels, then inspect further when a field’s representation or contents are uncertain.
Worked Example: Scanning a Small Policy Data Frame
A hypothetical analyst receives a five-row data frame with columns named contract, start_date, face_amount, and active. The analyst wants to verify the object’s basic structure before selecting fields for later work. The displayed classes are character, Date, numeric, and logical, respectively. How should the analyst use and interpret dplyr::glimpse(contracts)?
Step 1: Check the object size. The output reports five rows and four columns. That agrees with the description of the received data frame. This is a useful consistency check, but it does not establish that all five rows are unique contracts or that any particular values are correct.
Step 2: Match names to the intended fields. The four displayed names correspond to the expected identifier, date, amount, and status fields. If, for example, the date column appeared under an unexpected name such as start_dat, that would be a reason to investigate before writing code that refers to start_date.
Step 3: Assess the displayed classes. A Date class for start_date is plausible for calendar dates; numeric for face_amount permits numerical operations; and logical for active represents TRUE/FALSE values. Character for contract is also plausible because an identifier is generally a label, not a quantity to average.
Step 4: Treat the values as examples only. If the preview shows an amount such as 250000, that confirms only that a displayed entry has that appearance. It does not show that every amount is positive, that the amounts use the intended currency units, or that there are no missing entries in the unshown data.
Method and trap avoided. The method is a column-by-column structural scan. A common mistake is to call the displayed amount values the “range of face amounts.” They are only example values, so the minimum and maximum cannot be inferred unless the preview happens to include them and that has been verified separately.
How glimpse() and str() Differ
The base R function str() also inspects structure. Both functions can help identify an object’s dimensions, its components or columns, and their types. Their displays emphasize different things. For a data frame, glimpse() presents a concise, column-oriented scan that is convenient when names and types across many fields are the immediate concern. str() presents a structural view of the object and its components, which can be useful for seeing how a more complex or nested object is organized.
| Question | glimpse() | str() |
|---|---|---|
| What is the overall emphasis? | Compact, column-oriented scan | Structure and components of the object |
| Can it show data-frame columns and their types? | Yes | Yes |
| When is it convenient? | Scanning names and classes across a wide table | Inspecting an object’s structure, including components |
| Does the display prove all values are valid? | No | No |
Neither function replaces careful checks targeted to a question. A structural display cannot tell you by itself whether an unexpected value is a data-entry error, whether a factor has appropriate categories, or whether a date is within the intended observation period. It helps you decide what to inspect next; it does not make those decisions for you.
Worked Example: Choosing Between glimpse() and str()
An analyst has two objects. The first is a claims data frame with 80 columns, and the immediate question is whether expected fields such as claim_date and paid_amount are present and what classes they have. The second is a list containing a data frame and several separate components. The analyst needs to understand how the list is organized. Choose an inspection function for each question and explain why.
Claims data frame. Use dplyr::glimpse(claims) first. It provides a compact, column-oriented view, which suits scanning many names and classes. The analyst should check the output for claim_date and paid_amount, then interpret their displayed classes. This inspection does not confirm that claim dates are plausible or that paid amounts are correct.
Nested list. Use str(object) to inspect the list’s structure and components. The question concerns how the object is organized, not primarily a rapid scan across a wide table’s columns.
Method and trap avoided. The choice follows the inspection question: scan a wide table with glimpse(), or examine an object’s component structure with str(). A trap is to conclude that one function is universally more complete or more accurate. They are different structural views, and neither validates the underlying data.
Preview Values Are Not a Data Audit
The most consequential limitation of a compact scan is that displayed values are not the whole data set. The exact amount shown depends on the output’s available width and formatting. A preview may reveal a surprising value, but the absence of a surprise in the preview is not evidence that no such value occurs elsewhere.
Consider a hypothetical 200-row field called duration_months. Suppose the displayed preview contains five values, none of which is missing. It is incorrect to infer that the full column has zero missing values, or to calculate a missing-value rate as \(0/5=0\) and report it for all 200 rows. That calculation describes only the five displayed values, if those are indeed the five being counted. To calculate a column-wide rate, the analyst must count missing values across all 200 observations and divide by 200. glimpse() is not the count.
Similarly, seeing a few entries in a numeric column does not establish the full range, average, or frequency of any value. Those are questions for appropriate summaries or targeted checks. At this stage, keep the task narrow: use glimpse() to orient yourself to the data frame’s columns and types, and use what you notice to plan a specific follow-up inspection.
Call glimpse() on the intended data frame, using dplyr::glimpse(data) if you want to make the package explicit.
Check that the displayed size and fields are plausible for the object you meant to inspect.
Look for representations or displayed values that merit follow-up; do not treat the preview as exhaustive.
Use a suitable separate procedure to investigate a specific concern, rather than treating the structural scan as a complete validation.
Common Mistakes
- Reading the preview as the full column. Displayed values are examples; other rows may contain different values.
- Assuming a plausible class proves correct data. A numeric type permits arithmetic but does not prove units, validity, or meaning.
- Assuming a suspicious class proves an error. A character identifier may be appropriate, and the intended representation depends on the field’s role.
- Confusing inspection with validation. A compact structural display helps orient analysis; it does not certify data quality.
- Choosing a function without a question. Use the column-oriented scan of glimpse() when scanning a wide table is useful; use str() when the object’s structure is the focus.
For predictive analytics work, this small distinction matters. A model workflow depends on using the intended variables in suitable forms, but structural inspection is only an early orientation step. A sound interpretation names what the display shows—dimensions, names, classes, and a limited set of values—and avoids claiming more than that evidence supports. This approach is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
Check Your Understanding
For each situation, identify what glimpse() can establish and what it cannot establish from its display.
- A hypothetical data frame has 12 rows and four columns. A glimpse display reports those dimensions and shows a numeric class for exposure. What does the class establish, and what does it not establish? (Answer: It establishes that R currently represents exposure as numeric; it does not establish that the values, units, or all rows are valid.)
- A 300-row table’s preview shows no missing value in renewal_flag. Can the analyst conclude that the full column has no missing values? State the reason. (Answer: No; the preview is not necessarily the entire 300-row column.)
- An analyst wants to scan names and types in a data frame with 60 columns. Which function is the more direct first choice, glimpse() or str(), and why? (Answer: glimpse(), because its column-oriented display is convenient for scanning a wide table’s names and classes.)
- A list contains a data frame plus several separate components. The analyst’s question is how those components are organized. Which function is a suitable choice, and what does the result still not prove? (Answer: str() is suitable for inspecting the object’s structure; it does not prove that component values are correct.)
- A preview shows four values for a 100-row numeric field, and none is negative. Is it valid to conclude that the full field has no negative values? Explain. (Answer: No; negative values could occur in rows that are not shown.)