16) Create a logical variable by assignment.

This Blog entry is from the Getting Started in R section in Learn R.

Logical variables are True or False values which are derived by logical assignment.  To create a logical variable as the result of an evaluation assignment, start by creating a variable x by typing:

x <- 1
creating-a-numeric-variable-in-r-to-test-logically.png

Run the line of script to console:

numeric-variable-created-in-r-console.png

Create another variable y by typing:

y <- 2
creating-another-numeric-variable-to-test-against-in-r.png

Run the line of script to console:

another-numeric-variable-to-test-against-created-in-r-console.png

The logical variable will be created as the result of comparing one variable to another, in this case, questioning if x is greater than y.  Type:

Logical <- x > y
creating-a-logical-variable-by-assign-in-r.png

Run the line of script to the console:

logical-variable-created-by-assign-in-r-console.png

It can be seen that the variable Logical has been created and is available in the Environment pane:

a-logical-variable-has-been-created-in-environment-window-in-rstudio.png

Naturally, the variable can also be referenced via simply typing into the script editor:

Logical
a-logical-variable-ready-to-be-written-out-to-r-console.png

Run the script to console:

a-logical-variable-written-out-to-r-console.png

It can be seen that the variable has been written out as FALSE, in this instance, with the opposing value being TRUE.  Using the mode() function,  typing into the script editor:

mode(Logical)
testing-the-logical-variable-with-the-mode-function.png

Run the script to console:

return-value-of-mode-function-on-logical-variable-in-r-console.png

It can be seen that the variable writes out as being of type logical.