Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying R Object-oriented Programming
  • Table Of Contents Toc
  • Feedback & Rating feedback
R Object-oriented Programming

R Object-oriented Programming

By : Black
4.4 (9)
close
close
R Object-oriented Programming

R Object-oriented Programming

4.4 (9)
By: Black

Overview of this book

This book is designed for people with some experience in basic programming practices. It is also assumed that they have some basic experience using R and are familiar using the command line in an R environment. Our primary goal is to raise a beginner to a more advanced level to make him/her more comfortable creating programs and extending R to solve common problems.
Table of Contents (14 chapters)
close
close
4
4. Calculating Probabilities and Random Numbers
12
A. Package Management
13
Index

The workspace

The R environment keeps track of variables as well as allocates and manages memory as it is requested. One command to list the currently defined variables is the ls command. A variable can be deleted using the rm command. In the following example, the a and b variables have been changed, and the a variable is deleted:

> a <-  17.5
> b <- 99/4
> ls()
[1] "a" "b"
> objects()
[1] "a" "b"
> rm(a)
> ls()
[1] "b"

If you wish to delete all of the variables in the workspace, the list option in the rm command can be combined with the ls command, as follows:

> ls()
[1] "b"
> rm(list=ls())
> ls()
character(0)

A wide variety of other options are available. For example, there are directory options to show and set the current directory, as follows:

> getwd()
[1] "/home/black"
> setwd("/tmp")
> getwd()
[1] "/tmp"
> dir()
 [1] "antActivity.R"             "betterS3.R"               
 [3] "chiSquaredArea.R"          "firstS3.R"                
[5] "math100.csv"               "opsTesting.R"             
[7] "probabilityExampleOne.png" "s3.R"                      
[9] "s4Example.R"

Another important task is to save and load a workspace. The save and save.image commands can be used to save the current workspace. The save command allows you to save a particular variable, and the save.image command allows you to save the entire workspace. The usage of these commands is as follows:

> save(a,file="a.RData")
> save.image("wholeworkspace.Rdata")

These commands have a variety of options. For example, the ascii option is a commonly used option to ensure that the data file is in a (nearly) human-readable form. The help command can be used to get more details and see more of the options that are available. In the following example, the variable a is saved in a file, a.RData, and the file is saved in a human-readable format:

> save(a,file="a.RData",ascii=TRUE)
> save.image(" wholeworkspace.RData",ascii=TRUE)
> help(save)

As an alternative to the help command, the ? operator can also be used to get the help page for a given command. An additional command is the help.search command that is used to search the help files for a given string. The ?? operator is also available to perform a search for a given string.

The information in a file can be read back into the workspace using the load command:

> load("a.RData")
> ls()
[1] "a"
> a
[1] 19

Another question that arises with respect to a variable is how it is stored. The two commands to determine this are mode and storage.mode. You should try to use these commands for each of the data types described in the following subsections. Basically, these commands can make it easier to determine whether a variable is a numeric value or another basic data type.

The previous commands provide options for saving the values of the variables within a workspace. They do not save the commands that you have entered. These commands are referred to as the history within the R workspace, and you can save your history using the savehistory command. The history can be displayed using the history command, and the loadhistory command can be used to replay the commands in a file.

The last command given here is the command to quit, q(). Some people consider this to be the most important command because without it you would never be able to leave R. The rest of us are not sure why it is necessary.

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY