Create 0/1 Dummy from Logical Vector in R (Example Code)
In this R tutorial you’ll learn how to replace logicals by a 0/1 dummy indicator.
Example Data
my_logical <- c(FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE) # Exemplifying logical vector my_logical # Showing logical vector in RStudio console # [1] FALSE TRUE TRUE FALSE FALSE FALSE TRUE |
my_logical <- c(FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE) # Exemplifying logical vector my_logical # Showing logical vector in RStudio console # [1] FALSE TRUE TRUE FALSE FALSE FALSE TRUE
Example: Converting Logical to 0/1 Dummy Vector in R
as.numeric(my_logical) # Converting logical to 0/1 vector # [1] 0 1 1 0 0 0 1 |
as.numeric(my_logical) # Converting logical to 0/1 vector # [1] 0 1 1 0 0 0 1
Further Resources
You may find some related R programming language tutorials on topics such as variables, data conversion, and vectors below: