O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Logicals and characters

So far, we've only been dealing with numerics, but there are other atomic data types in R:

   > foo <- TRUE        # foo is of the logical data type 
   > class(foo)         # class() tells us the type 
   [1] "logical" 
   > bar <- "hi!"       # bar is of the character data type 
   > class(bar) 
   [1] "character" 

The logical data type (also called Booleans) can hold the values TRUE or FALSE or, equivalently, T or F. The familiar operators from Boolean algebra are defined for these types:

   > foo 
   [1] TRUE 
   > foo && TRUE                 # boolean and 
   [1] TRUE 
   > foo && FALSE 
   [1] FALSE 
   > foo || FALSE                # boolean or 
   [1] TRUE 
   > !foo                        # negation operator 
   [1] FALSE 

In a Boolean expression with a logical value and a number, any number that is not 0 is interpreted as TRUE:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required