Because R is designed heavily for statistics, handling numbers correctly is its strongest feature.
R distinguishes explicitly between different types of numbers for ultimate precision.
The numeric type is the most common data type for numbers in R.
It can handle both standard whole numbers and highly precise decimals (floating point).
The integer type strictly handles whole numbers, completely lacking decimal precision.
To explicitly create an integer variable, you must place an uppercase L immediately after the number!
# This creates a numeric variable x <- 10.5 y <- 55 # This creates a strict integer variable z <- 100Lprint(class(y)) # "numeric" print(class(z)) # "integer"
Sometimes you need to convert an integer into a numeric type, or vice-versa.
You can achieve this seamlessly using the as.numeric() or as.integer() functions.
This process is crucial when importing raw data that might be formatted incorrectly!
a <- 1L # integer # Convert integer to numeric b <- as.numeric(a)print(class(b)) # Outputs: "numeric"
What character must you append to a number to explicitly create a strict integer in R?
Which function is used to convert a variable into a numeric data type?