The as.double() function converts an input R object, such as integers or characters, to double-precision floating-point numbers.
Double-precision numbers provide higher precision than single-precision numbers, making them more efficient in numeric calculations.
as.double(obj, …)
Name | Value |
obj | It is an input R object to be converted into a double-precision floating number. |
… | Another potential argument. |
vec <- 1:5
float_vec <- as.double(vec) # 1 2 3 4 5
typeof(float_vec)
# Output: "double"
If a character vector represents a valid integer number, then as.double() function tries to coerce it into a double-precision number.
character_vec <- "21"
double_value <- as.double(character_vec)
print(double_value)
# Output: 21
print(typeof(double_value))
# Output: "double"
If you pass a character vector that is not a valid “numeric value”, it returns “NA” as output.
string_vec <- "KRUNAL"
double_val <- as.double(string_vec)
print(double_val)
# Output: NA
The as.double() function converts the TRUE boolean value to 1 and the FALSE boolean value to 0.
bool_true <- TRUE
bool_false <- FALSE
double_true <- as.double(bool_true)
double_false <- as.double(bool_false)
print(double_true)
# Output: 1
print(double_false)
# Output: 0
The factor is a categorical data type that has its levels.
If the factor levels are numeric, they will be converted to numeric values. However, if the levels are non-numeric, you might get unexpected results or errors.
factor_value <- factor(c("11", "21", "19"))
double_fact <- as.double(factor_value)
print(double_fact)
# Output: 11 21 19
The is.double() function checks if an input object is a type of “double”. It returns TRUE if the value has class “double” and FALSE otherwise.
is.double(obj)
Name | Value |
obj | It is an input R object to test and find whether it is of type “double.” |
num_vec <- 1:5
is_it_double <- as.double(num_vec)
is.double(is_it_double)
# Output: TRUE
Let’s pass a non-numeric value.
char_vec <- "Krunal"
is.double(char_vec)
# Output: FALSE
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…
R paste0() function concatenates strings without any separator between them. It is a shorthand version…
Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…
max() The max() function in R finds the maximum value of a vector or data…
The as.Date() function in R converts various types of date and time objects or character…
The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…