as.double & is.double Functions in R (2 Examples)

 

In this article, I’ll explain how to apply the as.double and is.double functions in the R programming language.

The article is structured as follows:

Sound good? Let’s dive right in!

Definitions & Basic R Syntaxes of as.double and is.double Functions

 

Definitions: You can find the definitions of the as.double and is.double functions below.

  • The as.double R function converts an integer to the double class.
  • The is.double R function tests whether a data object has the double class.

 

Basic R Syntaxes: Please find the basic R programming syntaxes of the as.double and is.double functions below.

as.double(values)               # Basic R syntax of as.double function
is.double(values)               # Basic R syntax of is.double function

 

I’ll show in the following two examples how to use the as.double and is.double functions in the R programming language.

 

Creation of Example Data

Consider the following example data:

x <- 1:5                        # Example vector
x                               # Print example vector
# 1 2 3 4 5

The previously shown output of the RStudio console shows the structure of our example data: It’s an integer vector ranging from 1 to 5.

 

Example 1: Convert Numeric/Integer to Class double

In Example 1, I’ll illustrate how to convert an integer to the double class in R using the as.double function.

x_double <- as.double(x)        # Apply as.double
x_double                        # Print converted vector
# 1 2 3 4 5

The previous output looks exactly the same as the original output. However, the data type was changed to the double class. In the next example I’ll show how to check that…

 

Example 2: Test whether Data Object has Class double

In this Example, I’ll illustrate how to check if a data object has the data type double. For this task, we can use the is.double function. Let’s first apply the is.double function to our original example vector:

is.double(x)                    # Apply is.double to integer
# FALSE

The RStudio console returns FALSE, i.e. the original vector does not have the double mode.

Let’s also apply the is.double function to our converted data:

is.double(x_double)             # Apply is.double to converted data
# TRUE

The RStudio console returns TRUE, indicating that the conversion to the double class worked!

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R codes of this post.

 

The YouTube video will be added soon.

 

Furthermore, you may read some of the related tutorials of this website. I have published numerous tutorials already.

 

Summary: In this R tutorial you learned how to use the as.double and is.double functions. In case you have any further questions, don’t hesitate to let me know in the comments.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top