Begin this practical exercise by setting the maximum line length in
R-Studio
to 80 characters. Go to RStudio
’s
Preferences
(or Global Options
under
Tools
) –> Code
–> Display
,
and tick the show margin
box. Make sure that the
margin column
is set to 80
mice
. Go to Tools
> Install Packages
in
RStudio
. If you are connected to the internet, select
Repository
under Install From
and type
mice
under Packages
. Leave the
Install to Library
at default and make sure that
Install Dependencies
is selected. Click install. If you are
not connected to the internet, select Package Archive File
under “Install from” and navigate to the respective file on your
drive.
Some packages depend on other packages, meaning that their functionality may be limited if their dependencies are not installed. Installing dependencies is therefor recommended, but internet connectivity is required.
If all is right, you will receive a message in the console that the package has been installed (as well as its dependencies).
ALternatively, if you know the name of the package you would like to
install - in this case mice
- you can also call
install.packages("mice")
in the console window.
mice
. Loading packages
can be done through functions library()
and
require()
.# type your code here
If you use require()
within a function, and the required
package is not available, require()
will yield a warning
and the remainder of the function is still executed, whereas
library()
will yield an error and terminate all executions.
The use of library()
when not doing too complicated things
is preferred - require()
would result in more computational
overhead because it calls library()
itself.
mammalsleep
dataset from package mice
by
typing mammalsleep
in the console, and subsequently by
using the function View()
. Using View()
is preferred for inspecting datasets that
are large. View()
opens the dataset in a spreadsheet-like
window (conform MS Excel, or SPSS). If you View()
your own
datasets, you can even edit the datasets’ contents.
mice
to the work directory (= the directory of your R project) as a
tab-delimited text file with .
as a decimal separator. Use
the function write.table()
and name the file
mammalsleep.txt
.# type your code here
mammalsleep.txt
file with
read.table()
. # type your code here
If you would like to know more about this dataset, you can open the
help for the mammalsleep
dataset in package
mice
through ?mammalsleep
.
Inspect the data set to obtain information about the variables, the number of observations. Some summary statistics.
# type your code here
Practical_C.RData
. Also, save the sleepdata file as a
separate workspace called Sleepdata.RData
. Now that we have imported our data, it may be wise to save the current workspace, i.e. the current state of affairs. Saving the workspace will leave everything as is, so that we can continue from this exact state at a later time, by simply opening the workspace file. To save everything in the current workspace, type:
# To save the entire workspace:
save.image("Practical_C.RData")
To save just the data set sleepdata
, and nothing else,
type:
# To save the data set only.
save(sleepdata, file = "Sleepdata.RData")
With the save functions, any object in the workspace can be saved.
sleepdata2
. Tip: use the square brackets to
indicate [rows, columns] or use the function filter()
from
dplyr
.# type your code here
# type your code here
To find out which animals have a brain weight larger than 1 standard deviation above the mean brain weight:
# type your code here
To plot these animals:
# type your code here
The downside is that it still prints all the animals on the x-axis.
This is due to the factor labels for species
being copied
to the smaller subset of the data. Plot automatically takes over the
labels. For example,
sleepdata2$species[which]
returns only 3 mammals, but still has 62 factor levels. To get rid of
the unused factor levels, we can use function factor()
:
sleepdata3 <- sleepdata2[which, ]
sleepdata3$species <- factor(sleepdata3$species)
sleepdata3$species
To plot the graph that we wanted:
# type your code here
End of exercise