18) Remove Variables in R.

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

In the event that long and complex scripts are being processed, where the objects might be using a substantial amount of memory (such as a large table from a database), it may be prudent to remove the objects when the script no longer needs it. 

To remove an object, the remove() function is used taking an argument as the name of the variable to be removed.  In this example, the String variable will be removed.  Type:

remove("String")
using-the-remove-function-in-a-r-script.png

Run the line of script to console:

remove-function-string-written-out-to-r-console.png

It can be seen that the String variable no longer appears in the environment pane:

remove-function-rstudio-environment-window.png

Naturally the variable will not be available in the session upon inspection of the ls() function.  Type:

ls()
r-script-with-ls-to-confirm-variable-removed.png

Run the line of script to console:

ls-function-written-out-to-r-console.png

It can be observed that the return is now minus the String variable.

17) List Variables in R

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

While RStudio will display the variables in the session at a given point in time,  the function can be replicated to console also.  The ls() function,  which has hitherto been used to identify the functions in a package, is by default used to identify objects in the session.  In the script editor,  type:

ls()
the-ls-function-being-used-in-an-r-script.png

Run the line of script to console:

the-ls-function-written-out-to-r-console.png

The variable names are returned to the console.  To reference these,  it is simply a matter of typing the variable name:

String
taking-a-return-value-from-the-ls-function.png

Run the line of script to console:

writing-a-string-value-out-to-r-console.png

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.

15) Create a string variable by assignment

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

Strings in R are surrounded by double quotation marks yet the assignment procedure is the same as numeric assignment.  Start by creating a new line in the script editor and typing:

Char <- "Test"
creating-a-string-variable-by-assign-in-r.png

Run the script to console:

string-variable-created-by-assign-in-rstudio-environment-window.png

The new String value is written to the Environment pane.  The variable is addressable from the script by typing the variable:

String
writing-out-a-string-variable-to-r-console.png

Run the line of script to console:

string-variable-written-out-to-r-console.png

Validate the variable type by using the mode() function. Type into the script pane:

mode(String)
using-the-mode-function-to-determine-string-variable.png

Run the line of script to console:

the-mode-function-return-value-in-r-console.png

It can be observed that the data type was defined as character upon assignment.

14) Creating a Numeric Variable by Assignment

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

Start by creating a new script:

creating-a-new-script-in-rstudio.png

A blank script window will be created that will be the target:

a-new-script-in-r-studio.png

Variables in R are created by assignment,  the process of setting a value.  The operator or command for assignment is the character combination of "<-".  To create and assign a numeric variable start by typing into the script window:

Numeric <- 1
creating-a-numeric-variable-by-assign-in-r-rstudio.png

Run the script to console:

r-console-numeric-variable-created-by-assign.png

A variable with the name Numeric has now been created.   It can be seen that RStudio has also recognised the creation of a new variable in the Environment Values pane towards the top right hand side of RStudio:

the-variable-environment-window-in-rstudio.png

script to console.  In this example,  create a new line in the script and type the name of the variable:

Numeric
write-numeric-variable-to-r-console.png

Run the line of script to console:

numeric-variable-written-out-to-r-console.png

It can be seen that the assigned value is written out.

The mode() function is intended to disclose the variable type, taking the variable name as the paramater.  Create a new line in the script editor and disclose the variable type, start by typing:

mode(Numeric)

Run the line of script to console:

using-the-mode-function-to-check-variable-type-in-r-console.png

It can be observed that the variable type has been returned as numeric.  It is also possible to assign a variable as the result of arithmetic or function output.  For example,  type into the script editor:

NumericResult <- 1 + 1
performing-some-basic-addition-on-numeric-variable-in-r-console.png

Run the line of script to the console:

numeric-variable-created-by-assign-and-addition-in-rstudio-variable-environment.png

It can be observed that the NumericVariable has been created and is available in the Environment Variables windows, and it would also return in the console when referencing the variable directly:

outputting-a-variable-to-r-console.png