Write a piece of R code that you could use to teach a seven-year-oldchild about Boolean logic. If you have never encountered Boolean logic before (sometimes it is called Boolean algebra), you will have to look it up to discover the three essential operators (AND, OR, NOT) and how they work. In addition, you will have to discover the "operators" (i.e., the special punctuation marks) that R uses to execute Boolean operations.

An Example: Baa-Baa Pasture

In my scenario, I want to sort sheep into different pastures based on their fleece color. Here are the coding steps that I need to accomplish this task:

  1. Create R objects for sheep that define their fleece colors
  2. Create R objects for pastures that define the fleece colors I want stored in the pasture
  3. Write three scenarios using operators (AND, OR, NOT) to return the statement "Welcome to your pasture" if the condition is TRUE and the statement "This isn't your pasture" if the conditional is FALSE

Step 1: Create R objects for sheep that define their fleece colors

In R language you can assign variables to objects using <- or =. So if betty has black fleece, I can assign the color black to betty using the following expression: betty_sheep <- "black". Vocalized as "betty_sheep gets black". Notice the quotation marks around the word black. "" helps R distinguish between characters and other objects in R e.g., numbers.

This following code assigns the fleece color of each sheep to a unique variable:

# This code assigns the fleece color of each sheep to a unique variable
betty_sheep <- "black"  
jeff_sheep <- "brown"  

After running the code above, anytime I type betty_sheep the string black is returned in the console.

Step 2: Create R objects for pastures that define the fleece colors

I need to define my pastures and the fleece color that each pasture will accept. In the code below, I defined two R objects (my pasture names) and their colors.

# This code assigns the color accepted in the two pastures
large_pasture <- "black"  
small_pasture <- "brown" 

After running the code above, anytime I type small_pasture the string brown is returned.

Step 3: Three scenarios using conditional operators (AND, OR, NOT)

Sort betty_sheep AND jeff_sheep into the large_pasture using the AND operator. If both sheep fleece colors match the color in large_pasture print "Welcome to your new home," but if one of the sheep fleece colors do not match the color in large_pasture print "This isn't your home".

This logic can be executed using a conditional expression (an if-else statement). The syntax of an if-else statement is:

if (test_expression) {
statement1
} else {
statement2
}

In Baa-Baa Pasture:

  • test_expression is: betty_sheep == large_pasture & jeff_sheep == large_pasture.
  • statement1 is: "Welcome to your new home"
  • statement2 is: "This isn't your home"

Note that in using the & logical operator, both expressions need to be true for the entire expression to be true.

Now we'll write a conditional expression (an if-else statement) that will print out the expected logic.

# AND requires both statements on either side of the operator to be true for the statement to be true

if (betty_sheep == large_pasture & jeff_sheep == large_pasture) 
print ("Welcome to your pasture") 
else print ("This isn't your pasture")

The expression on the left of the expression betty_sheep == large_pasture is TRUE, however jeff_sheep == large_pasture is FALSE. Thus, the expression betty_sheep == large_pasture & jeff_sheep == large_pasture is FALSE since, when using AND (&), both expressions need to be TRUE in order for the entire expression to be TRUE.

Start your scenario here:

Now it's your turn!

Take note of the line of code that explains the use of the AND operator. You will need to describe the use of the outcomes of OR and NOT. Keeping your seven-year-old in mind, write and submit the rest of the code and comments. You can develop your own scenario or use my sheep scenario above.

# Write a conditional that uses the AND logical operator


# Write a conditional that uses the OR logical operator


# Write a conditional that uses the NOT logical operator
LS0tCnRpdGxlOiAnTGFiIDE6IEJhc2ljIFIgY29kaW5nJwphdXRob3I6Ci0gWU9VUiBOQU1FCi0gWU9VUiBQQVJUTkVSUyBOQU1FCmRhdGU6ICJgciBTeXMudGltZSgpYCIKb3V0cHV0OgogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBwZGZfZG9jdW1lbnQ6IGRlZmF1bHQKLS0tCgpXcml0ZQlhCXBpZWNlCW9mCVIJY29kZSB0aGF0CXlvdQljb3VsZAl1c2UJdG8JdGVhY2gJYQlzZXZlbi15ZWFyLW9sZGNoaWxkCWFib3V0CUJvb2xlYW4JbG9naWMuIElmCXlvdQloYXZlIG5ldmVyIGVuY291bnRlcmVkIEJvb2xlYW4gbG9naWMgYmVmb3JlIChzb21ldGltZXMgaXQJaXMgY2FsbGVkCUJvb2xlYW4JIGFsZ2VicmEpLAl5b3UJd2lsbAloYXZlIHRvIGxvb2sgaXQgdXAgdG8gZGlzY292ZXIgdGhlIHRocmVlIGVzc2VudGlhbCBvcGVyYXRvcnMgKEFORCwJT1IsIE5PVCkgYW5kIGhvdyB0aGV5IHdvcmsuCUluIGFkZGl0aW9uLCB5b3Ugd2lsbAloYXZlIHRvCWRpc2NvdmVyIHRoZQ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