[R] Interdependencies of variable types, logical expressions and NA

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Thu Apr 28 09:07:40 CEST 2016


Hi All,

my script tries to do the following on factors:

> ## Check for case 3: Umsatz = 0 & Kunde = 1
> for (year in 2011:2015) {
+   Umsatz <- paste0("Umsatz_", year)
+   Kunde <- paste0("Kunde01_", year)
+   Check <- paste0("Check_U_0__Kd_1_", year)
+ 
+   cat('Creating', Check, 'from', Umsatz, "and", Kunde, '\n')
+ 
+   Kunden01[[ Check ]] <- ifelse(Kunden01[[ Umsatz ]] == 0 &
+                                 Kunden01[[ Kunde ]] == 1,
+                                 1, 0
+                                 )
+   Kunden01[[ Check ]] <- factor(Kunden01[[ Check ]],
+                                 levels=c(1, 0),
+                                 labels= c("Check 0", "OK")
+                                 )
+ 
+ }
Creating Check_U_0__Kd_1_2011 from Umsatz_2011 and Kunde01_2011 
Creating Check_U_0__Kd_1_2012 from Umsatz_2012 and Kunde01_2012 
Creating Check_U_0__Kd_1_2013 from Umsatz_2013 and Kunde01_2013 
Creating Check_U_0__Kd_1_2014 from Umsatz_2014 and Kunde01_2014 
Creating Check_U_0__Kd_1_2015 from Umsatz_2015 and Kunde01_2015 
> 
> table(Kunden01$Check_U_0__Kd_1_2011, useNA = "ifany")

Check 0      OK    <NA> 
      1      16      13 
> table(Kunden01$Check_U_0__Kd_1_2012, useNA = "ifany")

Check 0      OK    <NA> 
      1      17      12 
> table(Kunden01$Check_U_0__Kd_1_2013, useNA = "ifany")

Check 0      OK    <NA> 
      2      17      13 
> table(Kunden01$Check_U_0__Kd_1_2014, useNA = "ifany")

Check 0      OK    <NA> 
      1      15      14 
> table(Kunden01$Check_U_0__Kd_1_2015, useNA = "ifany")

Check 0      OK    <NA> 
      2      15      13 
> 
> Kunden01$Check_U_0__Kd_1_all <- ifelse(Kunden01$Check_U_0__Kd_1_2011 == 
1 |
+                                        Kunden01$Check_U_0__Kd_1_2012 == 
1 |
+                                        Kunden01$Check_U_0__Kd_1_2013 == 
1 |
+                                        Kunden01$Check_U_0__Kd_1_2014 == 
1 |
+                                        Kunden01$Check_U_0__Kd_1_2015 == 
1,
+                                        1, 0)
> 
> table(Kunden01$Check_U_0__Kd_1_all, useNA = "ifany")

    0  <NA> 
    7    23 

(Ann.: I made the values up. But the relations equal real world data.)

I had expected to get back a factor or at least a numeric variable 
containing 0, 1 and NA, instead 1 is not included.

I searched the web for information on the treatment of logical expressions 
when the data contains NA. I found:

1. 
https://stat.ethz.ch/R-manual/R-devel/library/base/html/NA.html
Examples
# Some logical operations do not return NA
c(TRUE, FALSE) & NA
c(TRUE, FALSE) | NA

2.
https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html
NA is a valid logical object. Where a component of x or y is NA, the 
result will be NA if the outcome is ambiguous. In other words NA & TRUE 
evaluates to NA, but NA & FALSE evaluates to FALSE. See the examples 
below. 

## construct truth tables :
x <- c(NA, FALSE, TRUE)
names(x) <- as.character(x)
outer(x, x, "&") ## AND table
outer(x, x, "|") ## OR  table
Ann. Not very useful. How should it be read?

3.
http://www.ats.ucla.edu/stat/r/faq/missing.htm
Good explanation for NA in general and in analysis, but no information 
about NA in logical expressions.

Then I made some tests with different data types and variables with NA:

-- cut --

# 2016-04-27-001_truth_table_for_logicals_and_NA.R

# Test 1
var2 <- c(TRUE, FALSE)
var3 <- c(NA, NA)
var1 <- c(1, 1)
ds <- data.frame(var1, var2, var3)
ds

ds$value_and_logical <- ifelse(ds$var1 | ds$var2, TRUE, FALSE)
ds$logical_and_na <- ifelse(ds$var2 | ds$var3, TRUE, FALSE)
ds$value_and_na <- ifelse(ds$var1 | ds$var3, TRUE, FALSE)

print(ds)
# Output
# var1  var2 var3 value_and_logical logical_and_na value_and_na
# 1    1  TRUE   NA              TRUE           TRUE         TRUE
# 2    1 FALSE   NA              TRUE             NA         TRUE

# Test 2
ds$var1 <- factor(ds$var1, levels = c(0, 1), labels = c("NOT ok", "OK"))
ds$var2 <- factor(ds$var2, levels = c(0, 1), labels = c("NOT ok", "OK"))
ds$var3 <- factor(ds$var3, levels = c(0, 1), labels = c("NOT ok", "OK"))

ds$value_and_logical <- ifelse(ds$var1 | ds$var2, TRUE, FALSE)
ds$logical_and_na <- ifelse(ds$var2 | ds$var3, TRUE, FALSE)
ds$value_and_na <- ifelse(ds$var1 | ds$var3, TRUE, FALSE)

# Output (abbrev.)
# Warning message:
#  In Ops.factor(ds$var1, ds$var3) : ?|? ist nicht sinnvoll für Faktoren

print(ds)
# Output
# var1 var2 var3 value_and_logical logical_and_na value_and_na
# 1   OK <NA> <NA>                NA             NA           NA
# 2   OK <NA> <NA>                NA             NA           NA

-- cut --

I had expected to get the same result in Test 2 as in Test 1.

Where can I find information and documentation about NA handling in 
logical expressions on different variable types?

Kind regards

Georg



More information about the R-help mailing list