Convert Logical to Dummy Vector in R (2 Examples)

 

In this tutorial, I’ll show how to replace logicals by a 0/1 dummy indicator in the R programming language.

The post will consist of this content:

Let’s get started.

 

Example 1: Convert Logical to Dummy Vector Using as.numeric Function

This example shows how to create a dummy vector based on a logical vector using the R programming language.

First, we have to create a logical vector in R:

x1 <- c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE)              # Create logical vector
x1                                                          # Print logical vector
# [1]  TRUE FALSE FALSE  TRUE FALSE  TRUE

The previous output of the RStudio console shows the structure of our logical variable. It is a sequence with a length of six containing TRUE and FALSE values.

If we want to convert this logical data object to a numeric 0/1 dummy, we can use the as.numeric function as shown below:

x1_dummy <- as.numeric(x1)                                  # Convert logical to dummy vector
x1_dummy                                                    # Print dummy vector
# [1] 1 0 0 1 0 1

The previous output of the RStudio console shows our final result – A 0/1 dummy indicator.

Please note that we could apply the same R code to a data frame column instead of a vector object.

However, in the next example I want to show you a typical problem when dealing with logical values. So keep on reading!

 

Example 2: Convert Logical Vector with Character Class to Dummy

A common issue with logical values is that the TRUE/FALSE values does not have the logical class, but the character class.

This is a problem that often occurs when you import data from an Excel or CSV file to R.

In this example, I’ll illustrate how to handle this problem in R.

First, we have to create another example vector:

x2 <- c("TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "TRUE")  # Create character vector
x2                                                          # Print character vector
# [1] "TRUE"  "FALSE" "FALSE" "TRUE"  "FALSE" "TRUE"

Have a look at the previous output: You can see based on the quotes, that our new example variable has the character class.

Let’s double-check that using the class function:

class(x2)                                                   # Check class of vector
# [1] "character"

The class function confirms our first guess: Our example vector has the character data type.

If we now want to convert our data to a dummy, we first need to convert our vector to the logical class using the as.logical function:

x2_logical <- as.logical(x2)                                # Convert character to logical
x2_logical                                                  # Print logical vector
# [1]  TRUE FALSE FALSE  TRUE FALSE  TRUE

Let’s check the class of our new data object:

class(x2_logical)                                           # Check class of updated vector
# [1] "logical"

Now, we are dealing with a real logical object.

In the next step, we can apply the as.numeric function as we already did in the first example:

x2_dummy <- as.numeric(x2_logical)                          # Convert logical to dummy vector
x2_dummy                                                    # Print dummy vector
# [1] 1 0 0 1 0 1

The final output is the same dummy indicator as in Example 1.

 

Video & Further Resources

If you need further info on the R programming syntax of this tutorial, you might want to watch the following video which I have published on my YouTube channel. In the video instruction, I show the content of this post in RStudio:

 

 

Furthermore, you might have a look at some of the other articles on my website. You can find some posts below:

 

To summarize: In this R programming tutorial you have learned how to convert a logical data object to a dummy vector. In case you have additional questions, let me know in the comments section.

 

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. Required fields are marked *

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

Top