Type Conversion
in R Programming
Learn to transform data between types using as.numeric, as.character, and as.logical: the essential toolkit for data wrangling in R.
What Are Data Types in R?
Every value in R has a type that determines how the computer stores it and what operations you can perform on it. The three most common atomic types are:
Why Convert Between Types?
Real-world data is messy. Numbers arrive as text in CSV files, survey responses come as "Yes" and "No" instead of TRUE/FALSE, and IDs that look numeric need to be treated as labels.
The R Type Hierarchy
The Three Core Functions
as.numeric() in Detail
as.numeric("3.14") # → 3.14
as.numeric("42") # → 42
# Logical to numeric
as.numeric(TRUE) # → 1
as.numeric(FALSE) # → 0
# Invalid conversion
as.numeric("hello") # → NA + warning
as.character() in Detail
as.character(42) # → "42"
as.character(3.14) # → "3.14"
# Logical to character
as.character(TRUE) # → "TRUE"
as.character(FALSE) # → "FALSE"
# Useful for labeling
x <- 100
paste("Score:", as.character(x))
as.logical() in Detail
as.logical(0) # → FALSE
as.logical(1) # → TRUE
as.logical(-7) # → TRUE
# Character to logical
as.logical("TRUE") # → TRUE
as.logical("false") # → FALSE
as.logical("yes") # → NA
Implicit vs Explicit Coercion
| Implicit Coercion | Explicit Coercion | |
|---|---|---|
| Who decides? | R decides automatically | You decide with as.*() |
| Direction | Always widens (logical → numeric → character) | Any direction you choose |
| Example | TRUE + 5 → 6 | as.numeric("5") → 5 |
| Risk | Silent: no warning | Warns you if NA produced |
| Best practice | Know it happens | Use this for clarity |
How to Apply Type Conversion
Example 1: CSV Import Fix
You import a CSV file and the price column is character. You need to compute the average.
prices <- c("19.99", "24.50", "9.99")
class(prices) # "character"
mean(prices) # Error!
# The fix
prices <- as.numeric(prices)
class(prices) # "numeric"
mean(prices) # 18.16
Example 2: Building Labels
You want to create a plot title that includes a numeric value.
score <- 97.5
label <- paste("Average:", as.character(score), "%")
# label is "Average: 97.5 %"
# Multiple values
ids <- c(101, 102, 103)
labels <- paste("Student", as.character(ids))
# "Student 101" "Student 102" "Student 103"
Example 3: Logical Filtering
A survey stored responses as 1 (yes) and 0 (no). You need TRUE/FALSE for filtering.
responses <- c(1, 0, 1, 1, 0)
# Convert to logical
agreed <- as.logical(responses)
# TRUE FALSE TRUE TRUE FALSE
# Now use in filtering
names <- c("Ana", "Ben", "Cal", "Dee", "Eve")
names[agreed]
# "Ana" "Cal" "Dee"
Complete Conversion Map
Common Mistakes to Avoid
Conversion Summary Table
| Input | as.numeric() | as.character() | as.logical() |
|---|---|---|---|
| TRUE | 1 | "TRUE" | TRUE |
| FALSE | 0 | "FALSE" | FALSE |
| 42 | 42 | "42" | TRUE |
| 0 | 0 | "0" | FALSE |
| "3.14" | 3.14 | "3.14" | NA |
| "hello" | NA | "hello" | NA |
Question 1 of 5
What does as.numeric("7.5") return in R?
Question 2 of 5
What is the result of as.logical(0)?
Question 3 of 5
What does as.numeric("apple") return?
Question 4 of 5
Which conversion always succeeds without producing NA?
Question 5 of 5
You run: x <- as.logical("yes"). What value does x hold?