R Basic

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

as.double()

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.

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.

Integer vector conversion

vec <- 1:5

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

typeof(float_vec) 

# Output: "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) 
# Output: 21

print(typeof(double_value)) 
# Output: "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) 
# Output: 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) 
# Output: 1

print(double_false) 
# Output: 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) 

# Output: 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.”

Example

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.

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

3 days ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

3 days ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

1 week ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

3 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

1 month ago