01 / 20
A B

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.

R Programming

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:

numeric
Numbers you can do math with: 3.14, 42, -7.5
character
Text strings wrapped in quotes: "hello", "42"
logical
Boolean values with only two options: TRUE or FALSE

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.

1
Import fixes: CSV files often read numbers as character strings.
2
Math operations: You cannot add "5" + "3" and get 8.
3
Logical filtering: Convert 0/1 columns to TRUE/FALSE for clean conditionals.

The R Type Hierarchy

logical numeric character implicit implicit TRUE / FALSE 42, 3.14 "hello", "42" ← narrower types automatically widen → explicit explicit

The Three Core Functions

as.numeric()
Converts to a number. "42" becomes 42. Non-numeric strings produce NA.
as.character()
Converts to text. 42 becomes "42". Works on any type.
as.logical()
Converts to TRUE/FALSE. 0 is FALSE, any non-zero number is TRUE.

as.numeric() in Detail

# Character to numeric
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

# Numeric to character
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

# Numeric to logical
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

1
Check the current type: use class(x) or is.numeric(x), is.character(x), is.logical(x).
2
Identify target type: what operation do you need? Math requires numeric, labels need character, filtering needs logical.
3
Apply the function: as.numeric(x), as.character(x), or as.logical(x).
4
Check for NA: use is.na() to verify no data was lost in conversion.
5
Store the result: assign back with x <- as.numeric(x).

Example 1: CSV Import Fix

You import a CSV file and the price column is character. You need to compute the average.

# The problem
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.

# Combining text with numbers
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.

# Survey data: 1 = agreed, 0 = disagreed
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

logical numeric character TRUE→1 FALSE→0 TRUE→"TRUE" FALSE→"FALSE" 0→FALSE else→TRUE "TRUE"→TRUE "yes"→NA 42 → "42" "42"→42 | "abc"→NA Safe (always works) Risky (may produce NA)

Common Mistakes to Avoid

✕ Forgetting Quotes Matter
Writing as.numeric(hello) instead of as.numeric("42"): without quotes, R looks for a variable named hello.
✕ Ignoring NA Warnings
as.numeric("$19.99") returns NA because the dollar sign is not a number. Always clean strings first.
✕ Assuming "yes" = TRUE
as.logical("yes") returns NA. Only "TRUE", "FALSE", "T", "F" are recognized.

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?