🧪 R Data Types – Understand and Use Core Data Types in R
🧲 Introduction – What Are Data Types in R?
In R, every piece of data—be it a number, text, or logical value—has a data type. Data types define the kind of data a variable holds and determine how operations are performed on them.
Understanding R’s core data types is essential for writing bug-free code, performing calculations, building data structures, and creating models.
🎯 In this guide, you’ll learn:
- The major data types in R
- How to check and convert data types
- Examples of usage in real-world R programs
🔤 Primary Data Types in R
🧪 Data Type | Example | Description |
---|---|---|
numeric | 4.5 , -12 | Default type for real numbers |
integer | 7L | Whole numbers with an L suffix |
character | "Hello" | Text strings |
logical | TRUE , FALSE | Boolean values |
complex | 2+3i | Numbers with real and imaginary parts |
raw | charToRaw("A") | Binary data |
📥 Assigning and Checking Data Types
R determines data types dynamically based on the assigned value.
x <- 3.14 # numeric
y <- 42L # integer
z <- "R Language" # character
is_valid <- TRUE # logical
🔎 Check the Type:
class(x) # "numeric"
typeof(y) # "integer"
is.character(z) # TRUE
🔁 Type Conversion (Casting)
You can explicitly convert values between data types using conversion functions:
Conversion Function | Converts To |
---|---|
as.numeric() | Numeric |
as.integer() | Integer |
as.character() | Character |
as.logical() | Logical |
as.complex() | Complex |
✅ Example:
x <- "25"
as.numeric(x) # Converts to numeric 25
as.integer(5.9) # Converts to integer 5
⚠️ Type Coercion in Vectors
When combining multiple data types in a vector, R automatically converts (coerces) them to the most flexible common type.
mixed <- c(1, "text", TRUE)
print(mixed)
🧾 Output:
[1] "1" "text" "TRUE"
🔁 All elements are coerced to character.
🧠 Coercion Hierarchy:
logical
< integer
< numeric
< complex
< character
🔄 Logical Data Type Example
a <- TRUE
b <- FALSE
result <- a & b
print(result)
🧾 Output:
[1] FALSE
🔌 Special Values in R
Special Value | Meaning |
---|---|
NA | Missing/undefined value |
NaN | Not a number (0/0, sqrt(-1)) |
Inf , -Inf | Positive/Negative infinity |
NULL | Empty or no value (not even NA) |
Example:
x <- NA
is.na(x) # TRUE
y <- 1 / 0
is.infinite(y) # TRUE
📌 Summary – Recap & Next Steps
Data types form the foundation of every R operation—from simple math to complex data processing. Understanding them helps you write robust and efficient R code.
🔍 Key Takeaways:
- R supports numeric, integer, character, logical, complex, and raw types
- Use
class()
andtypeof()
to inspect variables - Use
as.*()
functions to convert between types - Coercion occurs when mixing types—plan accordingly
- Handle missing or special values with care (
NA
,NaN
,NULL
)
⚙️ Real-World Relevance:
Correct handling of data types is critical in data cleaning, transformations, statistical modeling, and machine learning workflows using R.
❓ FAQs – R Data Types
❓ How do I know if a number is stored as integer in R?
✅ Use is.integer(x)
. Also, integer values must have an L
suffix:
x <- 10L
is.integer(x) # TRUE
❓ What is the default numeric type in R?
✅ numeric
(also known as “double-precision float”).
❓ Can a vector store mixed data types?
✅ No. R will coerce all elements to a common type. Use a list
if you need to store mixed types.
❓ What is the difference between NA and NULL?
✅ NA
represents a missing value; NULL
represents nothing (no value, no object).
❓ How do I convert a logical to numeric?
✅ Use as.numeric(TRUE)
➝ 1
, as.numeric(FALSE)
➝ 0
.
Share Now :