# 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.

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.

## 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 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 #  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 2 3 4 5 6 7 8 9 10 # attr(,"Csingle") #  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  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 #  0 0 0 0 0 # attr(,"Csingle") #  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.

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