2013-05-30 17 views
13

non possono comprendere le proprietà di logica (boolean) valori TRUE, FALSE e NA quando viene utilizzato con logica OR (|) e logica AND (&). Ecco alcuni esempi:operatori logici (AND, OR) con NA, TRUE e FALSE

NA | TRUE 
# [1] TRUE 

NA | FALSE 
# [1] NA 

NA & TRUE 
# [1] NA 

NA & FALSE 
# [1] FALSE 

Puoi spiegare queste uscite?

risposta

20

di citare ?Logic:

NA è un oggetto logico valido. Se un componente di x o y è NA, il risultato di sarà NA se il risultato è ambiguo. In altre parole NA & TRUE restituisce NA, ma NA & FALSE viene valutato su FALSE. Vedi gli esempi di seguito.

La chiave è la parola "ambiguo". NA rappresenta qualcosa che è "sconosciuto". Quindi NA & TRUE potrebbe essere vero o falso, ma non lo sappiamo. Considerando che NA & FALSE sarà falso indipendentemente dal valore mancante.

10

E 'spiegata in help("|"):

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

Dagli esempi in help("|"):

>  x <- c(NA, FALSE, TRUE) 
>  names(x) <- as.character(x) 
>  outer(x, x, "&")## AND table 
     <NA> FALSE TRUE 
<NA>  NA FALSE NA 
FALSE FALSE FALSE FALSE 
TRUE  NA FALSE TRUE 
>  outer(x, x, "|")## OR table 
     <NA> FALSE TRUE 
<NA> NA NA TRUE 
FALSE NA FALSE TRUE 
TRUE TRUE TRUE TRUE 
Problemi correlati