Appendix H. R Functions Introduced in This Book
Tải bản đầy đủ
read.txt()
Read a file in .txt format and create a data frame.
read.xport()
Read an SAS XPORT file (foreign package).
readWorksheetFromFile()
Read an Excel spreadsheet (XLConnect package).
save()
Write an R object to the working directory or a specified file.
xmlToDataFrame()
Read an XML file (XML package).
Datasets
attach()
Select a particular dataset for the following analysis.
data()
Determine what datasets are available (with no argument) or load a
dataset.
data.frame()
Combine two or more vectors to make a new data frame.
detach()
Deselect a dataset; that is, following commands no longer analyze
that dataset.
edit()
Edit a dataset.
fix()
Edit a dataset.
head()
View selected lines of a dataset, from the top.
str()
Determine the structure of an object.
subset()
Create a subset of a specified data frame.
298

Appendix H: R Functions Introduced in This Book
tail()
View selected lines of a dataset, from the bottom.
Graphical Functions 1—Creates Graph
barplot()
Produce a bar plot.
bland.altman.ade()
Produce a BlandAltman plot (epade package).
boxplot()
Produce a box plot.
coplot()
Produce a coplot (conditioning plot).
cor.plot()
Produce a corrgram (psych package).
corrplot()
Produce a corrgram (corrplot package).
dotchart()
Produce a dot chart.
dotplot.mtb()
Produce a dot chart as in Minitab (plotrix package).
ehplot()
Produce an EngelmannHeckerPlot (plotrix package).
fan.plot()
Produce a fan plot (plotrix package).
ggpairs()
Produce a generalized pairs plot (GGally package).
ggplot()
Produce many types of plots (ggplot2 package).
ggscatmat()
Produce a scatter plot matrix with corr coefficients on top (GGally
package).
gpairs()
Produce a generalized pairs plot (gpairs package).
R Functions Introduced in This Book

299
grid()
Draw a grid on the current plot.
heatmap()
Produce a heat map.
heatmap.2()
Produce an enhanced heat map (gplots package).
hexbin()
Produce a hexbin plot (hexbin package).
hist()
Produce a histogram.
Hist()
Produce a histogram for multiple groups (RcmdrMisc package).
histbackback()
Produce backtoback histograms (Hmisc package).
histogram()
Produce histograms for multiple groups (lattice package).
histStack()
Produce a stacked histogram (plotrix package).
image()
Create a heat map.
levelplot()
Produce a falsecolor plot (lattice package).
missiogram()
Produce a plot of missing values (epade package).
missmap()
Produce a plot of missing values (Amelia package).
mosaic()
Produce a mosaic plot (vcd package).
mosaicplot()
Produce a mosaic plot.
pairs()
Create a scatter plot matrix.
300

Appendix H: R Functions Introduced in This Book
pie()
Produce a pie chart.
pie3D()
Produce a threedimensional pie chart (plotrix package).
plot()
Produce a scatter plot or other plots.
PlotBubble()
Produce a bubble plot (DescTools package).
pyramid()
Produce a pyramid plot (epicalc package).
qq()
Produce a quantilequantile (QQ) plot (lattice package).
qqnorm()
Produce a QQ plot with theoretical quantiles.
qqplot()
Produce a QQ plot.
scatter3d()
Produce a threedimensional scatter plot with a regression surface
(car package).
scatterplot()
Produce a scatter plot with advanced features (car package).
scatterplot3d()
Produce a threedimensional scatter plot (scatterplot3d pack‐
age).
scatterplotMatrix()
Produce a scatter plot matrix with advanced features (car package);
also ++spm()++.
scatter.ade()
Produce a scatter plot with advanced features (epade package).
smoothScatter()
Produce a smooth scatter plot.
spineplot()
Produce a spine plot (spinogram).
R Functions Introduced in This Book

301
stem()
Produce a stemandleaf plot.
stem.leaf()
Produce an advanced stemandleaf plot (aplpack package).
stripchart()
Produce a strip chart.
sunflowerplot()
Produce a sunflower plot.
xyplot()
Produce a scatter plot (lattice package).
Graphical Functions 2—Adds Features to
Existing Graph
abline()
Draw a straight line on an existing graph.
axis()
Add an axis to the current plot.
legend()
Add a legend on the current plot.
lines()
Put curved lines on the current plot.
mtext()
Put text in the margins of the current plot.
par()
Set graphical parameters, or query about same.
plotmath
See ?plotmath to include math expressions on graphs.
points()
Draw points on the current plot.
polygon()
Draw/fill a polygon.
302

Appendix H: R Functions Introduced in This Book
qqline()
Add a line to a QQ plot.
rug()
Draw a rug plot on the current plot.
text()
Put text in the plotting area of the current plot.
Miscellaneous
asTheEconomist()
Style imitator for lattice graphs (latticeExtra package).
c()
Combine the arguments, forming a vector.
cat()
For printing output from functions.
colors()
Give R color names.
demo()
Run a demonstration of selected R capabilities.
dev.off()
Complete writing to a graphical device and save a file.
jpeg()
Open a file to be saved in .jpeg format; must conclude with
dev.off().
order()
Reorder the rows of a data frame by the values of a selected vari‐
able.
par()
Set graphical parameters, or query about same.
png()
Open a file to be saved in .png format; must conclude with
dev.off().
print()
Print output.
R Functions Introduced in This Book

303
rgl.snapshot()
Save a screenshot as a .png file (rgl package).
Packages
available.packages()
Check what packages are available for download.
install.packages()
Download and install one or more R packages.
installed.packages()
Check what packages are installed on a computer.
library()
Load a previously installed package into the current R session.
Statistics
R has many statistics functions not covered in this book. The ones
we looked at include:
aggregate()
Divide data into subsets, computing summary statistics for each
subset.
cor()
Calculate the Pearson correlation coefficient.
CrossTable()
Produce a contingency table in SPSS or SAS format (gmodels pack‐
age).
density()
Compute kernel density estimates.
dist()
Compute the distance between rows of a matrix.
ecdf()
Compute an empirical cumulative distribution function.
Ecdf()
Compute an empirical cumulative distribution function (Hmisc
package).
304

Appendix H: R Functions Introduced in This Book
hclust()
Perform a hierarchical cluster analysis.
lm()
Compute a linear model (e.g., a regression).
max()
Compute the maximum value of a vector.
mean()
Compute the mean of a vector.
median()
Compute the median of a vector.
min()
Compute the minimum value of a vector.
quantile()
Find quantiles of a vector.
scale()
Center and/or scale columns of a matrix.
sd()
Compute the standard deviation of a vector.
summary()
Compute several summary statistics of a vector.
table()
Compute oneway or twoway frequencies.
var()
Compute the variance of a vector.
UserDefined Functions and Scripts
function() {}
Create a userdefined function.
source()
Execute a script.
R Functions Introduced in This Book

305
Workspace and Directories
ls()
Determine what objects are in the current workspace.
getwd()
Find the current working directory.
setwd()
Change the working directory .
306

Appendix H: R Functions Introduced in This Book
Index
Symbols
# (octothorpe), in comments, 8
% (percent sign)
%% remainder operator, 2
%/% (divide and round down)
operator, 2
() (parentheses)
grouping with, 2
in functions, 3
troubleshooting in R code, 289
* (multiplication) operator, 2
** (exponent) operator, 2
+ (addition) operator, 2
, (comma) in R code, 292
 (subtraction) operator, 2
/ (division) operator, 2
3D (see threedimensional plots)
< operator, 4
using to create vectors, 13
? (question mark), help command
shortcut, 4, 46
[] (bracket) operator in R, 134, 209
^ (exponent) operator, 2
~ (tilde), in grouping variables, 56, 92
A
a:b notation (subsets), 134
abline() function, 74
adding regression line in scatter
plot, 133
active plot, 131
agglomeration, using in clustering,
222
aggregate() function, 108
using to get vector of mean salar‐
ies, 112
airquality dataset, 9
ANCdata dataset, 235
aplpack package, 84
arguments (function), 3
abbreviations of, differences
among packages, 294
arithmetic operators, 2
array() function, 237
ASCII files, 271
aspect ratio, 174
asTheEconomist() function, 143
attach() function, 10
available.packages() function, 7
axis() function, 54
B
backgrounds, nonwhite, 100
bandwidth, 97
choosing, 98
banking, 174
baplot() userdefined function, 163,
168
shorter version of, 169
bar charts (see bar plots)
bar plots, 105114
bar spacing and orientation, 111
four variants of a graph, 113
comparing to pie chart of invest‐
ment portfolio, 119
307
faculty ranks, salaries, and sex,
107
in generalized pairs matrix, 198
showing relationship between
variables
stacked bar plot, 108
spine plot, 109
barcode plots, 197
barplot() function, 106
space argument, 113
bias, 165
bins, 83, 85
different numbers of in sbp histo‐
grams, effects of, 87
exercise, experimenting with
number, 94
number of, leaving to R, 86
sizing in histograms, 87
BlandAltman plot, 161169
comparison of two measurements,
167
produced using epade package,
167
bland.altman.ade() function, 167
blood pressures dataset (see sbp data‐
set)
box plots, 6779
adding a reference line, 74
comparison of progressively more
detailed charts, 71
improving appearance of, 75
commands used, 77
of math achievement scores, 69
of Nimrod dataset
performance time by level and
medium, 73
box type (bty) parameter, 52
boxandwhiskers plots, 69
boxes around graphs, 53
BP (blood pressure) dataset, 37
bracket operator ([]), 134, 209
breaks = argument, hist() function,
86
bty (box type) parameter, 52
bubble plots, 206212
of income, related to education
and IQ, 208
308

Index
of trees data produced by Plot‐
Bubble(), 207
relating IQ, gender, and education
in subset of data, 211
C
car package
scatter3d() function, 203
scatterplot() function, 145
scatterplotMatrix() function, 186
case sensitivity in R, 4
errors, using wrong case, 289
categorical variables, 19
and quantitative variables in gen‐
eralized pairs matrix, 195198
graphing, 235245
in ANCdata dataset, 235
causation versus correlation, 78
church denominations dataset
(ex1713), 183
codebook, summary of, 184
scatterplot matrix of, 184
clustering, 221233
agglomerative and divisive meth‐
ods for, 222
dendrograms, 221
of clusters in mtcars dataset,
226
heat maps, 227
combining with a dendro‐
gram, 231
produced with heatmap.2()
from gplots, 232
coalash dataset, 206
codebook (datasets), 18
for ex1713 (church denomina‐
tions), 184
col (color) argument, stripchart, 54
colors
adding color to points in 3D scat‐
ter plot, 201
adding to mosaic plots, 241
in heat maps, 227
in R, quick reference on, 257
listing and viewing colors in R, 54
range of, controlling in image()
function, 228
using in correlation matrix, 193