Page 219 - Data Science class 11
P. 219

c.  Logical Operators
            Logical operators as supported by the R language are presented herewith. They are applicable only to vectors of the
            logical, numeric, or complex type. Remember that all numbers greater than 1 are taken as logical operators and are
            considered as logical values of TRUE.
            Each element of the first vector is compared with the corresponding element of the second vector. The result of the
            comparison is a Boolean value.
                1.   & Logical AND operator: It is  called "Element-wise Logical AND operator." It combines each element of
                    the first vector with the corresponding element of the second vector and gives an output of TRUE if both
                    the elements are TRUE.
            Example

            Enter the following code snippet:

                first <- c(4,2,TRUE,5+6i)
                second <- c(7,2,FALSE,5+6i)
                print(first & second)
            The following result is produced:
















                2.   Logical OR operator: It is called Element-wise Logical OR operator. It combines each element of the first
                    vector with the corresponding element of the second vector and gives an output of TRUE if one of the
                    elements is TRUE. Remember that the symbol for the OR operator is | (known as the pipe symbol). Don’t type
                    / instead of it.
            Example

            Enter the following code snippet:
                first <- c(4,0,TRUE,5+6i)
                second <- c(7,0,FALSE,6+7i)
                print(first|second)
            The following result is produced:
















                3.   Logical NOT operator: It takes each element of the vector and gives the opposite logical value.
            Example

            Enter the following code snippet:

                                                                                         Programming with R    217
   214   215   216   217   218   219   220   221   222   223   224