7 Logicals (TRUE/FALSE) variables

Data Wrangling Recipes in R: Hilary Watt

7.1 Convert to logical: recodes 0 to false and other numbers to TRUE. NA values and characters variables are coded to NA.

table(anaemia$plts, exclude = NULL)
anaemia$plts.logic <- as.logical(anaemia$plts)
summary(anaemia$plts.logic)
table(anaemia$plts, anaemia$plts.logic, exclude = NULL)
## Note that negative numbers are also coded to TRUE
as.logical(-7) # non-zero numbers result in TRUE
as.logical(0.0000002345) # non-zero numbers result in TRUE
as.logical(-0.0000002345) # non-zero numbers result in TRUE
as.logical("fdlkfj") # text/ character variables result in NA
as.logical("4")  # because 4 is in quotes, it is treated as a character variable, and results in NA
as.logical(NA)  # because 4 is in quotes, it is treated as a character variable, and results in NA

7.2 Evaluating expression to get logical (binary) var: TRUE/ FALSE/ NA

Expressions that evaluate to TRUE or FALSE or NA can be use to generate a logical variable. Use double equals “==” for “evaluated to see whether or not it is equal to”. The following evaluates expression on right (for each observation/ row), and determines whether it is TRUE or FALSE or NA. Result is a logical data-type.

#  create  new  variable  called  “elective”  with  == “evaluate to see whether 
# equal to”
anaemia$elective <- anaemia$operat == "Elective"
table(anaemia$elective, exclude = NULL)  # check newly created variable
table(anaemia$elective, anaemia$operat, exclude = NULL)  # check newly created variable against original variable
#
summary(anaemia$hb_pre ) # inspect variable
anaemia$high_hb_pre <-  (anaemia$hb_pre > 150 ) # evaluate expression to TRUE/ FALSE
summary(anaemia$high_hb_pre) # see result
summary(anaemia$hb_pre [ anaemia$high_hb_pre==TRUE]) # check new variable to old variable
summary(anaemia$hb_pre [ anaemia$high_hb_pre== FALSE]) # check new variable to old variable

The following can also be used in expressions that evaluate to TRUE/ FALSE/ NA.

Logical
Operation Description
& and
| or
! not
Relational (numeric and string)
Operation Description
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal
!= not equal

Note that if_else can be used more flexibly instead to result in TRUE/ FALSE/ NA as desired. (or Base R’s ifelse. However, if_else from library(dplyr) from tidyverse retains the format of dates and factors and is more natural to use in many situations).

7.3 Logicals to numeric format, where zero evaluates to FALSE, other numbers to TRUE, characters to NA.

Sometimes logicals evaluate using TRUE=1, FALSE=0, within some R functions. To convert directly to binary 0/1 integer format:

#  create  new  variable  called  “elective”  with  == “evaluate to see whether 
# equal to”
anaemia$elective <- as.integer(anaemia$operat == "Elective") # evaluate expression to TRUE/ FALSE then to integer 1/0
table(anaemia$elective, anaemia$operat, exclude = NULL) # check new variable to old variable
#
summary(anaemia$hb_pre ) # inspect variable
anaemia$high_hb_pre <- as.integer (anaemia$hb_pre > 150 ) # evaluate expression to TRUE/ FALSE then to integer 1/0
summary(anaemia$high_hb_pre) # see result
summary(anaemia$hb_pre [ anaemia$high_hb_pre==1]) # check new variable to old variable
summary(anaemia$hb_pre [ anaemia$high_hb_pre== 0]) # check new variable to old variable

The main dataset is called anaemia, available here: https://github.com/hcwatt/data_wrangling_open.

Data Wrangling Recipes in R: Hilary Watt. PCPH, Imperial College London.