0
0
R-programmingHow-ToBeginner · 2 min read

R How to Convert Data Types with Examples

In R, convert data types using functions like as.numeric(), as.character(), as.factor(), and as.logical() to change a value's type.
📋

Examples

Input"123"
Output123 (numeric)
Input123
Output"123" (character)
Inputc("apple", "banana")
Outputfactor with levels apple, banana
🧠

How to Think About It

To convert data types in R, think about what type you want your data to be, then use the matching conversion function like as.numeric() for numbers or as.character() for text. This changes the data's form so R treats it correctly.
📐

Algorithm

1
Identify the current data type of your value.
2
Decide the target data type you want to convert to.
3
Use the appropriate conversion function like <code>as.numeric()</code> or <code>as.character()</code>.
4
Store or use the converted value as needed.
💻

Code

r
x <- "42"
num_x <- as.numeric(x)
char_x <- as.character(num_x)
factor_x <- as.factor(c("apple", "banana", "apple"))
print(num_x)
print(char_x)
print(factor_x)
Output
[1] 42 [1] "42" [1] apple banana apple Levels: apple banana
🔍

Dry Run

Let's trace converting a string "42" to numeric and back to character, then creating a factor.

1

Convert string to numeric

Input: "42" -> as.numeric("42") = 42

2

Convert numeric to character

Input: 42 -> as.character(42) = "42"

3

Create factor from character vector

Input: c("apple", "banana", "apple") -> as.factor(...) creates factor with levels apple and banana

StepInputOutput
1"42"42 (numeric)
242"42" (character)
3c("apple", "banana", "apple")factor with levels apple, banana
💡

Why This Works

Step 1: Use as.numeric() to convert text to number

The as.numeric() function changes a character string that looks like a number into an actual numeric value R can calculate with.

Step 2: Use as.character() to convert number to text

The as.character() function turns numbers into strings so R treats them as text.

Step 3: Use as.factor() to create categorical data

The as.factor() function converts a vector of strings into a factor, which R uses to represent categories with levels.

🔄

Alternative Approaches

Using type.convert()
r
x <- "123"
converted <- type.convert(x, as.is = TRUE)
print(converted)
Automatically converts to the most appropriate type but less explicit than as.numeric() or as.character().
Using as.integer() for integer conversion
r
x <- "42"
int_x <- as.integer(x)
print(int_x)
Converts to integer type, which is a specific numeric type without decimals.

Complexity: O(n) time, O(n) space

Time Complexity

Conversion functions process each element once, so time grows linearly with data size.

Space Complexity

Conversions create new objects of the same size, so space usage is proportional to input size.

Which Approach is Fastest?

Direct functions like as.numeric() are fast and clear; type.convert() is convenient but slightly slower due to type guessing.

ApproachTimeSpaceBest For
as.numeric()/as.character()/as.factor()O(n)O(n)Explicit, clear conversions
type.convert()O(n)O(n)Automatic type detection
as.integer()O(n)O(n)Integer-specific conversion
💡
Always check your data type with class() after conversion to confirm it changed as expected.
⚠️
Trying to convert non-numeric strings with as.numeric() results in NA and a warning.