R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Logical Operators Out of Class Notes Week 3

SEED VALUE is a starting point for the random number generator’s algorithm. If you set the seed to a specific value before generating random numbers, you ensure that you get the same sequence of random numbers every time you run your code with that seed.

set.seed(32139)
x <- runif(5) ; y <- runif(5) #generates numeric vectors x and y, each containing 5 random numbers 
print(x)
## [1] 0.83104946 0.64676549 0.31785331 0.09997367 0.72402349
print(y)
## [1] 0.4311398 0.5633632 0.1665925 0.9740164 0.9836434

Performs the comparison x < y, it checks element-wise if each element of x is less than the corresponding element of y

Is 0.83104946 (element 1 of x) less than 0.4311398 (element 1 of y)? No, so the first element of v is FALSE.

v <- x < y
print(v)
## [1] FALSE FALSE FALSE  TRUE  TRUE

w <- x >= 1/2: This line creates another logical vector w by comparing each element of vector x with the scalar value 1/2.

x is the same numeric vector as before. 1/2 is a scalar value, which is equivalent to 0.5. When you perform the comparison x >= 0.5, it checks element-wise if each element of x is greater than or equal to 0.5. Here’s the element-wise comparison: Is 0.83104946 (element 1 of x) greater than or equal to 0.5? Yes, so the first element of w is TRUE.

w <- x >= 1/2
print(w)
## [1]  TRUE  TRUE FALSE FALSE  TRUE

Negation: When you apply the ! (logical NOT) operator to v, it negates the logical values in the vector. In other words, it flips TRUE to FALSE and FALSE to TRUE

print(v)
## [1] FALSE FALSE FALSE  TRUE  TRUE
print(!v)
## [1]  TRUE  TRUE  TRUE FALSE FALSE

And / Or / Exclusive Or

## An element in the result is TRUE if either the corresponding element in v or the corresponding element in w is TRUE

v | w  
## [1]  TRUE  TRUE FALSE  TRUE  TRUE
print(v | w)
## [1]  TRUE  TRUE FALSE  TRUE  TRUE
## An element in the result is TRUE only if both the corresponding element in v and the corresponding element in w are TRUE
v & w
## [1] FALSE FALSE FALSE FALSE  TRUE
print(v & w)
## [1] FALSE FALSE FALSE FALSE  TRUE
## An element in the result is TRUE if either v or w is TRUE, but not both.
xor(v, w)
## [1]  TRUE  TRUE FALSE  TRUE FALSE
print(xor(v, w))
## [1]  TRUE  TRUE FALSE  TRUE FALSE

Summarizing a Logical Vector

## Logical vector x with 10 elements, where TRUE indicates that the corresponding element in the random numbers vector is greater than 0, and FALSE indicates that it is not.
x <- (rnorm(10) > 0)
print(x)
##  [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
table(x) ## Prints summary
## x
## FALSE  TRUE 
##     6     4
print(table(x))
## x
## FALSE  TRUE 
##     6     4

All / Any

x <- c(TRUE, TRUE) 
y <- c(TRUE, FALSE)

all(x)
## [1] TRUE
any(x)
## [1] TRUE
all(y)
## [1] FALSE
any(y)
## [1] TRUE
print(all(x))
## [1] TRUE
print(any(x))
## [1] TRUE
print(all(y))
## [1] FALSE
print(any(y))
## [1] TRUE