Tutorials › Exam PA - Predictive Analytics › Inspecting Data Frame Structure with str()

Exam PA · Data Structures and Exploratory Summaries · Tutorial 101 of 966

Inspecting Data Frame Structure with str()

Use str() to read a data frame’s compact structural report and identify how R currently represents its columns.

Beginner 9 min read

What You'll Learn

  • Read the observation and variable counts reported for a data frame
  • Locate each column name, class label, and abbreviated value preview
  • Distinguish a preview from a complete listing of the data
  • Identify when an observed class or value pattern merits follow-up
  • Explain the limits of str() as a data-quality check

Why Inspect a Data Frame’s Structure?

Before calculating summaries or fitting a predictive model, an analyst needs to know what R has actually read into memory. A column intended to record claim amounts might have been imported as text; a date might appear as a number or character string; a category might be represented by a different class than expected. These are not merely display details: the current representation can affect which operations are available and how later analysis behaves.

The base R function str() provides a compact structural report. For a data frame, it reports the object type, the number of observations and variables, and a brief description of each column. It also prints a small preview of values. The function is useful for an initial inspection, but it does not establish that the data are correct, complete, or suitable for a particular model.

Definition: For a data frame, str(x) displays a compact description of the object’s structure: its reported dimensions, the names and current classes of its columns, and abbreviated previews of column values. The preview is not a complete printout of the data frame.

Reading the Main Parts of the Output

Suppose a hypothetical analyst has a data frame named renewals. Calling str(renewals) may produce output in this form:

'data.frame': 5 obs. of 4 variables:
$ record_id: int 201 202 203 204 205
$ region: chr "North" "South" "North" "West" ...
$ renewed: logi TRUE FALSE TRUE TRUE FALSE
$ premium: num 640 710 590 830 760

The opening line identifies the object as a data frame and reports 5 observations and 4 variables. In this context, observations correspond to rows and variables correspond to columns. The following lines describe those columns individually. Each begins with a dollar sign and the column name, followed by a short class label and some values.

Labels such as int, num, chr, and logi are compact indicators of how R currently represents a column. In this output, they signal integer, numeric, character, and logical representations, respectively. The printed values are examples drawn from the column, not definitions of the variable’s business meaning. For instance, a column printed as num could represent dollars, years, or a coded quantity; the output alone does not tell you which.

The exact amount of output depends on the object and display settings. A longer data frame will not ordinarily have every row listed in the structural report. The ellipsis (...) indicates that the displayed sequence has been abbreviated. Likewise, if a particular value does not appear in the preview, that does not show that it is absent from the full column.

1
Read the object summary.
Check that the output identifies the expected object type, then note the reported number of observations and variables.
2
Scan the column descriptions.
For each variable, read its name and the class label shown by R.
3
Interpret the preview cautiously.
Use the sample values to notice plausible patterns or concerns, but do not treat the preview as a complete audit.
4
Decide what needs checking next.
If the representation or displayed values are unexpected, record the issue for a more targeted check rather than assuming the data are valid.

Worked Examples

Worked Example: Reviewing a Renewal Data Frame

A hypothetical insurer has imported a data frame with renewal records. The analyst runs str(renewals) and sees:

'data.frame': 5 obs. of 4 variables:
$ record_id: int 201 202 203 204 205
$ region: chr "North" "South" "North" "West" ...
$ renewed: logi TRUE FALSE TRUE TRUE FALSE
$ premium: num 640 710 590 830 760

Step 1: Read the dimensions. The opening line reports 5 observations and 4 variables. Thus the object currently has five rows and four columns.

Step 2: Match names to representations. There is a column named record_id, shown as int; region, shown as chr; renewed, shown as logi; and premium, shown as num.

Step 3: Interpret only what is supported. The preview suggests that region contains text labels, renewed contains TRUE/FALSE values, and premium contains numeric values. The output does not establish that all five premiums are positive, that every region is valid, or that the identifier is unique. Those claims require checks beyond this display.

Conclusion. The structure is a useful first look and its reported dimensions match a five-record, four-column expectation. The analyst should still confirm the contents and quality of the columns before relying on them. This method avoids treating a plausible preview as proof that the entire data set is correct.

Worked Example: Spotting a Representation Worth Investigating

A hypothetical analyst expects a numeric loss amount and runs str(losses). The output is:

'data.frame': 4 obs. of 3 variables:
$ case: int 31 32 33 34
$ loss: chr "1200" "950" "not recorded" "1600"
$ open: logi TRUE TRUE FALSE TRUE

Step 1: Establish the size. R reports 4 observations and 3 variables, which is consistent with four records and three columns.

Step 2: Compare the expected and observed representations. The column named loss is shown as chr, not as a numeric representation. Its preview contains digit strings as well as the text "not recorded". This is direct evidence that R currently represents the displayed entries as character data.

Step 3: State the implication without overreaching. A numeric loss calculation cannot safely assume that the column is already numeric. The output does not tell us how a missing or unrecorded value ought to be handled, nor does it show whether other entries contain unexpected text. Those are data-definition and data-cleaning questions to resolve before analysis.

Conclusion. Flag loss for follow-up and confirm the intended treatment of its non-numeric entry. Do not infer that all entries are numeric just because most of the preview looks like digits. The method avoids confusing a number written as text with a numeric column.

Worked Example: Separating Column Names from Sample Values

A hypothetical portfolio data frame has this structural report:

'data.frame': 6 obs. of 3 variables:
$ segment: Factor w/ 3 levels "Basic","Plus",..: 1 2 1 3 2 1
$ term: int 12 12 6 12 6 6
$ amount: num 450 520 390 680 500 410

Step 1: Count the structure. The report says there are six observations and three variables.

Step 2: Identify the variable names. The names are segment, term, and amount. The numbers printed after a column name are not additional column names; they are part of the description or preview of that one column.

Step 3: Read the representations as reported. R labels segment as a factor, term as an integer, and amount as numeric. For the factor, the report includes level information and displays internal codes in the preview. Those codes should not be mistaken for the category labels: the level names shown in the output are the relevant clue about the labels R associates with that factor.

Step 4: Note the limits. The output only partially displays the level names and the preview is short. It does not prove that the factor has no additional issues or that the integer values in term use the intended unit.

Conclusion. The structural report helps distinguish the variable names, their current representations, and sample contents. The method avoids interpreting factor codes as if they were the category names or treating an integer representation as proof of correct units.

What str() Can and Cannot Establish

The central benefit of str() is that it makes an object’s current organization visible at a glance. It can reveal a mistaken import format, an unexpected column count, or values that warrant investigation. In predictive work, this matters because methods expect inputs in suitable forms. A column intended for numerical calculation but represented as text, for example, is a reason to pause before proceeding.

However, structure is not the same as data quality. A compact report does not verify that row identifiers are unique, that values fall within acceptable ranges, that missing values are absent, or that category labels are consistent throughout the data. It also cannot tell you whether a column’s business interpretation is correct. Those questions require checks chosen for the data and the analysis purpose.

The output is also a description of the object as it currently exists in R, not a guarantee about how the data were intended to be stored. An analyst should compare the displayed names, dimensions, classes, and sample values with the data specification or the expectations for the import. If anything does not agree, investigate before making changes or fitting a model.

Key takeaway: Use str() for a compact first inspection: verify the reported row and column counts, scan each column’s name and class, and look at the abbreviated values. Treat anything surprising as a prompt for a more specific check—not as a complete diagnosis.

Common Mistakes

  • Assuming the preview lists every row. It is abbreviated. A value not shown may still occur elsewhere in the column.
  • Confusing a class label with a business definition. num indicates a current representation, not whether the variable is dollars, age, or some other quantity.
  • Reading displayed factor codes as category names. When a factor is printed with codes, use the reported levels to understand the labels rather than assigning meaning to the codes themselves.
  • Treating a plausible structure as a clean data set. Correct-looking dimensions and a few expected values do not validate every row or field.

Check Your Understanding

For each question, distinguish what the structural report confirms from what still needs further checking.

  1. str(applications) begins with 'data.frame': 8 obs. of 5 variables:. How many rows and columns are reported? (Answer: 8 rows and 5 columns.)
  2. A column is shown as amount: chr "300" "450" ..., although the analyst expected amounts for arithmetic. What is the current representation, and what should the analyst conclude? (Answer: It is currently character; investigate the column and its entries before assuming it is ready for numeric calculations.)
  3. A report shows status: logi TRUE FALSE TRUE. Does this preview prove that the entire column has only those three displayed values? (Answer: No; the preview is abbreviated and does not establish all values in the column.)
  4. A factor column’s preview shows codes 1 2 1 and the report lists levels "Standard","Extended". What should be used to interpret the categories? (Answer: The listed level labels, not the numeric codes treated as category names.)
  5. A report shows the expected number of rows and columns and plausible sample values. Does that establish that there are no missing values or invalid records? (Answer: No; str() is a structural overview, not a complete missing-value or validity check.)
Clinical Biostats