R Basic

as.double() and is.double() Functions in R Language

Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision numeric format.

Double-precision numbers provide higher precision than single-precision numbers making it more efficient in numeric calculations.

That’s where as.double() and is.double() functions come into the picture.

as.double()

The as.double() function converts an input R object, such as integers or characters, to double-precision floating point numbers. 

Syntax

as.double(obj, …)

Parameters

Name Value
obj It is an input R object to be converted into a double-precision floating number.
Another potential argument.

Visual representation

Integer vector conversion

vec <- 1:5
float_vec <- as.double(vec) # 1 2 3 4 5

float_vec
typeof(float_vec) # "double"

Character vector

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) # 21

print(typeof(double_value)) # "double"

Non-numeric Conversion

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) # NA

Logical vector

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) # 1
print(double_false) # 0

Factor

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) # 11 21 19

is.double()

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.

Syntax

is.double(obj)

Parameters

Name Value
obj It is an input R object to test and find whether it is of type “double.”

Visual representation

Example

num_vec <- 1:5

is_it_double <- as.double(num_vec)

is.double(is_it_double) # TRUE

Let’s pass non-numeric value.

char_vec <- "Krunal"

is.double(char_vec) # FALSE

That’s it.

Recent Posts

What is is.factor() Function in R

R consists of various data types, and "factor" is one of them. You can use…

9 hours ago

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R…

1 day ago

Checking If a Data Frame is Empty in R

What criteria are being evaluated to determine if a data frame is empty? There is…

2 days ago

How to Check If a List is Empty in R

What do we mean when we say an empty list? An empty list does not…

3 days ago

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…

4 days ago

What is is.complex() function in R

If you are working with imaginary components, you might want to find whether you are…

1 week ago