single & as.single Functions in R (2 Examples)

 

In this article you’ll learn how to create a single-precision vector using the single and as.single functions in the R programming language.

Table of contents:

Let’s do this!

 

Important Notes on Single-Precision Objects in R Programming

Before we jump into the R code, it is important to be aware of the following statement that you can find in the help documentation of the double function:

R has no single precision data type. All real numbers are stored in double precision format. The functions as.single and single are identical to as.double and double except they set the attribute Csingle that is used in the .C and .Fortran interface, and they are intended only to be used in that context.

Please keep this in mind when reading the following examples of this page.

 

Example Data

At first, we’ll need to construct some example data:

x_double <- as.double(1:10)         # Create double-precision vector
x_double                            # Print double-precision vector
#  [1]  1  2  3  4  5  6  7  8  9 10

Have a look at the previous output of the RStudio console. It shows that the exemplifying data is a vector containing ten elements.

We can check whether our data object is a single- or double-precision vector using the is.double function:

is.double(x_double)                 # Test double-precision vector
# [1] TRUE

The RStudio console returns TRUE, i.e. our example data is a double-precision object.

 

Example 1: Convert Double-Precision Vector to Single Using as.single() Function

This example demonstrates how to convert a double-precision object to a single-precision object.

For this, we can apply the as.single function as shown below:

x_single1 <- as.single(x_double)    # Convert to single precision vector
x_single1                           # Print single precision vector
#  [1]  1  2  3  4  5  6  7  8  9 10
# attr(,"Csingle")
# [1] TRUE

The previous R code has created a new data object called x_single1. This data object contains the same values as our input data object.

However, in addition to the values our new data object also contains the attribute $Csingle [1] TRUE.

 

Example 2: Create Single-Precision vector Using single() Function

The R programming code below explains how to create a single-precision object from scratch.

We can do that using the single function as shown below:

x_single2 <- single(length = 5)     # Create single-precision vector
x_single2                           # Print single-precision vector
# [1] 0 0 0 0 0
# attr(,"Csingle")
# [1] TRUE

 

Video, Further Resources & Summary

Do you need further explanations on the R codes of this article? Then you could watch the following video on my YouTube channel. In the video, I’m explaining the R codes of this article in a live session.

 

The YouTube video will be added soon.

 

In addition, you could have a look at the related tutorials on this website:

 

Summary: In this R programming tutorial you have learned how to apply the single and as.single functions. If you have further questions or comments, don’t hesitate to tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

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

Menu
Top