01 / 21
>_

print() & str()
in R Programming

Learn to display output and inspect object structure using R's two most essential debugging and exploration functions.

R Programming

Why Inspect Objects?

Every R variable holds data, but you can't always see what's inside just by looking at your code. Inspecting objects lets you verify values, understand data types, and catch bugs before they cascade into errors.

What is print()?

print() displays the value of an R object in the console. It's how R shows you data: numbers, text, vectors, data frames, and more.

print("Hello, R!")
[1] "Hello, R!"

What is str()?

str() stands for "structure." It reveals the internal blueprint of any R object: data type, dimensions, and a compact preview of the contents.

x <- c(10, 20, 30) str(x)
num [1:3] 10 20 30

The R Object Model

R Object Value What it looks like print() Structure What it is internally str() Type & Class num, chr, logi, list... Every R variable is an object with value, type, and structure

print() reveals the value side. str() reveals the structural side. Together, they give you the complete picture.

The print() Function

Syntax
print(x, ...)
Returns
Invisibly returns x (same object)
Side Effect
Displays the value in the console
# Vectors print(c(1, 2, 3)) # [1] 1 2 3 # Character print("R is great") # [1] "R is great" # Logical print(TRUE) # [1] TRUE

The str() Function

Syntax
str(object, ...)
Returns
NULL (invisibly); output is a side effect
Purpose
Compact display of internal structure
df <- data.frame( name = c("Alice", "Bob"), score = c(95, 87) ) str(df)
'data.frame': 2 obs. of 2 variables: $ name : chr "Alice" "Bob" $ score: num 95 87

print() vs str(): The Key Difference

print(df) name score 1 Alice 95 2 Bob 87 Shows the DATA Human-readable format "What does it look like?" str(df) 'data.frame': 2 obs. $ name : chr $ score: num Shows the BLUEPRINT Type-focused, compact "What is it made of?" vs

How to Apply Both Functions

1
Create or load your object. This could be a vector, data frame, list, or any R variable.
2
Call str() first to understand the type, dimensions, and column types before doing anything else.
3
Call print() to verify values. Confirm the actual data matches what you expect.
4
Use print() inside loops and functions. Without explicit print(), output is silently suppressed.
5
Debug with both. When something goes wrong, str() often reveals type mismatches that print() alone won't show.

Example 1: Inspecting a Vector

temps <- c(72.1, 68.5, 75.3, 69.9, 71.0) print(temps) str(temps)
[1] 72.1 68.5 75.3 69.9 71.0 num [1:5] 72.1 68.5 75.3 69.9 71.0

Example 2: Inspecting a List

student <- list( name = "Maya", grades = c(92, 88, 95), active = TRUE ) str(student)
List of 3 $ name : chr "Maya" $ grades: num [1:3] 92 88 95 $ active: logi TRUE

Example 3: Inspecting a Data Frame

survey <- data.frame( id = 1:4, rating = c(4.5, 3.8, 4.9, 2.1), agree = c(TRUE, FALSE, TRUE, FALSE) ) print(survey) str(survey)
id rating agree 1 1 4.5 TRUE 2 2 3.8 FALSE 3 3 4.9 TRUE 4 4 2.1 FALSE 'data.frame': 4 obs. of 3 variables: $ id : int 1 2 3 4 $ rating: num 4.5 3.8 4.9 2.1 $ agree : logi TRUE FALSE TRUE FALSE

Auto-Printing vs Explicit print()

# In the console, typing a variable auto-prints it: x <- 42 x # auto-prints: [1] 42 # Inside a for-loop, auto-print does NOT work: for (i in 1:3) { i # SILENT: nothing printed! } # You MUST use print() explicitly: for (i in 1:3) { print(i) }
[1] 1 [1] 2 [1] 3

When to Use Which Function

What do you need to know? Values print() Display in loops Show results to user Verify a computation Structure str() Check column types Explore new datasets Debug type errors Both! Use together for complete understanding

Common Mistakes

✕ Forgetting print() in loops
Writing x inside a for-loop and expecting output. Without print(x), nothing appears in the console.
✕ Confusing str() with string
str() means "structure", not "string." To convert to a string, use as.character() or toString().
✕ Using print() to debug types
The output for a number (e.g., 42) and a string ("42") can look similar. Only str() unambiguously reveals the type difference.

Summary Comparison

print() str()
Purpose Display values Display structure
Shows types? No Yes
Shows dimensions? Partially Yes
Needed in loops? Yes Rarely
Best for Showing results Exploring data
Output style Formatted, readable Compact, technical

Question 1 of 5

What does the str() function in R stand for?

Question 2 of 5

What happens when you type x (without print()) inside a for-loop in R?

Question 3 of 5

In the str() output above, what does chr [1:3] tell you?

Question 4 of 5

You receive a data frame but don't know what columns it has or their types. Which function should you call first?

Question 5 of 5

How should the student fix the function so it displays each squared value?