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.

12) Use the help() function to explain a function

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

If using RStudio, navigating to the documentation via the help pane is by far the easiest and most intuitive means to access help.  Taking the output of functions recalled in previous Blog entries,  navigation to help can be triggered by invoking the help function.

To navigate to help,  click on the console input cursor:

all-functions-in-the-ggplot2-package-for-help.png

Type:

help("ggplot")
help-function-in-r-to-get-help-on-ggplot2.png

Press the Enter key to execute the line of script:

ggplot2-help-written-out-to-rstudio-help-window.png

While operating in RStudio,  the help will be displayed in the dedicated help pane.  If operating in the console,  the experience would be that the same text is written out to the console in text only.  It follows that help() exists for the purposes of making help and documentation available universally in R.

11) List all Functions in a Package.

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

Once a package is loaded, beyond using the help, an understanding of all the functions available to the package can be obtained.   Although a script active, console passive approach is advocated this Blog entry is one of the few occasions where it is more appropriate to use the console directly rather than clutter up the script.

Click on the console window in the bottom left hand corner of RStudio:

typing-directly-into-the-r-console.png

Type directly into the console:

ls("package:ggplot2")
ls-ready-to-be-written-out-in-the-r-console.png

Press the Enter key to execute the script:

all-the-functions-in-the-ggplot2-package-written-to-the-r-console.png

A list of all functions in the package is returned.

8) Review Help and Documentation.

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

In the packages pane a list of all packages installed has been presented. Having followed previous Blog entries we know that ggplot2 is now installed.  It is customary for packages to carry good documentation,  which can be accessed by clicking on the hyperlink overlaying the package name:

using-rstudio-to-navigate-to-help-via-link.png
ggplot2-link-in-rstudio.png

Clicking on the link immediate navigation to the packages documentation takes place:

package-help-in-r-being-displayed-in-rstudio.png

This feature provides a more intuitive means to navigate to documentation for functions. In this example,  scroll down and clock on the function link autoplot:

r-ggplot2-function-help.png

It can be seen that the documentation is displayed, navigating away from the index.