3/26/02 11:24 AM File = D:\r\useful.doc 1
John Miyamoto
3/26/02 11:24 AM File = D:\r\useful.doc 1
Useful R Commands (Top); Useful R Information Sources (Bottom)
Contents (Cntrl-left click on a link to jump to the corresponding section)
Section / Topic1RefTo: s1 / Useful R Functions
2RefTo: s0001 / Notes re plot and lines
3RefTo: s1b / Regular expressions in R
4RefTo: s6 / Special characters like "\n"
5RefTo: s2 / Useful Linksand Documents (Bookmarks: 's2', 'Links', 'Docs')
6RefTo: s5 / Notes re the Built-In R Editor
7RefTo: s3 / Possible Editors for R Programming
8RefTo: s4 / Notes on R-Code/Computing (see also 'e:\r\notes' and 'e:\r\docs')
1s1. Useful R Functions
Function / Purpose"imputation" / See packages 'cat', 'norm', and 'mix' for various imputation functions. See, also, the 'transcan' function in the 'Hmisc' package. See, also,
%in%
match / If X and Y are vectors, X %in% Y is a vector. length(X %in% Y) = length(X). The i-th element of X %in% Y equals TRUE if X[i] is an element of Y and is FALSE otherwise. The match function serves similar purposes but is more easily modified.
.libPaths / .libPaths gets/sets the library trees within which packages are looked for. '.libPaths(new)' where 'new' is a character vector with the locations of R library trees.
.Platform
R.version
Sys.info() / .Platform is a list with some details of the platform under which R was built. This provides means to write OS portable R code. E.g., '.Platform$Os.type' gives the operating system that the current version was written for. See also 'R.version' and 'Sys.info()'.
<-, -> / The operators <- and -> cause a search to made through the environment for an existing definition of the variable being assigned. If such a variable is found then its value is redefined, otherwise assignment takes place globally. Note that their semantics differ from that in the S language, but is useful in conjunction with the scoping rules of R.
all.equal / all.equal(x,y) is a utility to compare R objects x and y testing ``near equality''. If they are different, comparison is still made to some extent, and a report of the differences is returned. Don't use 'all.equal' directly in if expressions—either use 'identical' or combine the two, as shown in the documentation for 'identical'. See also 'identical' and '=='.
AnalyzeFMRI / Package for analyzing fMRI images.
arrows / Draw arrows between pairs of points. Also useful for creating error bars (set 'angle' to 90).
barplot / Makes bar plots. Note that 'x.loc <- barplot(....)' stores that x-axis midpoints of each bar in a vector.
barplot2 / Makes error bars in barplots (in 'gregmisc' package, 'gplots' functions)
basename / 'basename' removes all of the path up to the last path separator (if any). See also 'dirname'.
bmp
jpeg
png / Output a graphics object to file in bmp, jpeg (jpg) and png formats.
body / Get or set the body of a function.
'body(v.names)' returns the code that defines the 'v.names' function.
tmpfun <- function(a, b=2){}
body(tmpfun) <- quote({z <- a + b; z^2})
tmpfun(3)
[1] 25
bquote / bquotequotes its argument except that terms wrapped in.()are evaluated in the specifiedwhereenvironment.
bringToTop / bringToTop brings the specified screen device's window to the front of the window stack (and gives it focus). With argument -1, it brings the console to the top. This function is useful for manipulating the screen appearance of R output (including graphics output).
browser / Interrupt the execution of an expression and allow the inspection of the environment where browser was called from. Entering 'Q' exits from the browser.
by / Function by is an object-oriented wrapper for tapply applied to data frames. A data frame is split by row into data frames subsetted by the values of one or more factors, and function FUN is applied to each subset in term.
choose(n, k) / Functions choose and lchoose return binomial coefficients and their logarithms.
choose.files / Use a Windows file dialog to choose a list of zero or more files interactively.
citation / Gives examples and explanation for how to cite R and R-packages.
colors
rainbow
heat.colors
terrain.colors
topo.colors
cm.colors / 'colors' returns the built-in color names which R knows about.
The other functions create a vector of n “contiguous” colors. Conceptually, all of these functions actually use (parts of) a line cut out of the 3-dimensional color space, parametrized by hsv(h,s,v, gamma), where gamma=1 for the foo.colors function, and hence, equispaced hues in RGB space tend to cluster at the red, green and blue primaries. Some applications such as contouring require a palette of colors which do not “wrap around” to give a final color close to the starting one.
colSums
rowSums
colMeans
rowMeans / Compute row and column sums and means for numeric arrays.
colSums, rowSums, colMeans, rowMeans / These functions are equivalent to use of apply with FUN = mean or FUN = sum with appropriate margins, but are a lot faster. As they are written for speed, they blur over some of the subtleties of NaN and NA. If na.rm = FALSE and either NaN or NA appears in a sum, the result will be one of NaN or NA, but which might be platform-dependent.
convert.mod.w2o,
Win2OpenBUGS / These functions convert a WinBUGS model files to OpenBUGS model files. convert.mod.w2o allows the user to write a comment at the head of the resulting OpenBUGS model file; typically, this comment indicates the WinBUGS model that was the source of the OpenBUGS model file, although the user can write any comment that he or she chooses.
count.fields / count.fields counts the number of fields, as separated by 'sep', in each of the lines of file read. This used to be used by 'read.table' . Useful for debugging.
csimint
csimtest
simint
simtest / Multiple comparison procedures for anova (oneway anova only?) in the package multcomp.
curve / Draws a curve corresponding to the given function or expression (in x) over the interval [from,to].
density / The (S3) generic function density computes kernel density estimates. Its default method does so with the given kernel and bandwidth for univariate observations. Computes an estimated density function from a univariate sample of data.
diag / Extract or replace the diagonal of a matrix, or construct a diagonal matrix.
dir / Retrieve file information on a Windows machine. (Like "dir" in Dos.)
dirname / 'dirname' returns the part of the path up to (but excluding) the last path separator, or "." if there is no path separator.
do.call / Executes a function call from the name of the function and a list of arguments to be passed to it. E.g., do.call("rbind", list.of.frames) applies 'rbind' to the dataframes in the list.
dput / Writes an ASCII text representation of an R object to a file or connection, or uses one to recreate the object. dput opens file and deparses the object x into that file. The object name is not written (contrary to dump). If x is a function the associated environment is stripped. Hence scoping information can be lost. See also 'dump'.
dump / This function takes a vector of names of R objects and produces text representations of the objects on a file or connection. A dump file can usually be sourced into another R (or S) session. See also 'dput'.
duplicated / Determines which elements of a vector or data frame are duplicates of elements with smaller subscripts, and returns a logical vector indicating which elements (rows) are duplicates.
eigen / Computes eigenvalues and eigenvectors.
escape: \n, \t, \b, \r, \\,\" / escape characters in character strings; '\n' is newline; '\t' is tab; '\b' is backspace; '\r' is carriage return; '\\' is single backslash; '\"' is single quote. See ?Quotes for more escape characters.
eval
evalq
eval.parent
with / Evaluate an R expression in a specified environment.
eval(parse(text=string)) / If 'string' is a character vector that represents a valid R command, e.g., 'string <- "tm1 <- 3*4" ', then 'eval(parse(text=string))' has the same effect as executing the command that is represented syntactically in 'string'. 'source()' builds on 'parse()' and 'eval()', i.e.,
parse() : character --> expression
eval () : expression --> [evaluated result].
expand.grid / Create a data frame from all combinations of the supplied vectors or factors.
E.g., 'expand.grid(1:3, 1:4)' creates a dataframe with 12 rows, containing all combinations of {1, 2, 3} with {1, 2, 3, 4}.
file.choose / Choose a file interactively. User specifies file to be opened or created by means of a dialog box.
file.create, file.exists, file.remove, file.rename, file.append, file.copy, dir.create / Functions for creating files, testing for existence of files, removing files, renaming files, appending to files, copying files, and creating directories.
file.exists / Checks whether a file exists in a specified directory.
file.info / Utility function to extract information about files on the user's file systems.
file.path / Construct the path to a file from components in a platform-independent way. Useful for writing platform-independent code that includes references to specific files.
Usage: file.path(..., fsep = .Platform$file.sep)
Arguments: ... = character vectors; fsep = the path separator to use.
Value: A character vector of the arguments concatenated term-by-term and separated by fsep if all arguments have positive length; otherwise, an empty character vector. E.g., file.path("c:/mydata", "project.rda")
filter / 'ts' package: Computes running means.
fitdistr / MASS package: Maximum-likelihood fitting of univariate distributions, allowing parameters to be held fixed if desired.
fractions / Computes the integers ratio that produces the decimal representation of the ratio, e.g., fractions(.3333) returns "1/3".
get / Search for an R object with a given name and return it. Useful for retrieving an R object from a different environment on the search path or for retrieving an R object with a name that is constructed by a function or program.
getwd() / Returns the absolute filename representing the current working directory of the R process.
gl / Generate factors by specifying the pattern of their levels. E.g.,
> gl(3,4)
[1] 1 1 1 1 2 2 2 2 3 3 3 3
Levels: 1 2 3
glm( ..., family = binomial) / Logistic regression. Note that the summary function gives results in the log odds ratio form. You need to transform back to odds ratios.
head / Returns the first or last parts of a vector, matrix, data frame or function. Useful for viewing or extracting a small subset of cases.
head / Returns the first few rows of a dataframe or matrix (or vector).
help.search / Searches the R help system for documentation matching a given character string in the (file) name, alias, title, concept or keyword entries (or any combination thereof), using either fuzzy matching or regular expression matching. Names and titles of the matched help entries are displayed nicely. See also 'RSiteSearch'.
identical / The safe and reliable way to test two objects for being exactly equal. It returns TRUE in this case, FALSE in every other case. See also 'all.equal' and '=='.
ifelse(test, yes, no) / 'ifelse' returns a value with the same shape as test which is filled with elements selected from either 'yes' or 'no' depending on whether the element of 'test' is TRUE or FALSE.
image / Creates a grid of colored or gray-scale rectangles with colors corresponding to the values in z. This can be used to display three-dimensional or spatial data aka images.
image / Creates a grid of colored or gray-scale rectangles with colors corresponding to the values in z. This can be used to display three-dimensional or spatial data aka “images”. This is a generic function. The functions heat.colors, terrain.colors and topo.colors create heat-spectrum (red to white) and topographical color schemes suitable for displaying ordered data, with n giving the number of colors desired.
interaction.plot / Makes a line plot for a 2-way anova design with one factor on the X axis and the other factor with separate lines.
isoMDS (in MASS)
sammon (in MASS)
cmdscale (in mva)
xgvis (in xgobi) / Alternative functions that perform nonmetric multidimensional scaling (mds).
layout / Used for making multiple graphs on a single screen (like mfrow or mfcol). Unlike mfrow or mfcol, row and column heights can be specified for individual rows or columns.
library('package name')
library(help = MASS)
ls(package:MASS) / Loads 'package name'.
Lists all objects in MASS along with short description.
Lists all objects in MASS if it is attached.
lines / Add lines and points to a plot. See Section 2RefTo: s0001 for more info.
list.files / This function produces a list containing the names of files in the named directory. dir is an alias. key = filenames
logtrans / MASS package: Find and optionally plot the marginal likelihood for alpha for a transformation model of the form log(y + alpha) ~ x1 + x2 + ....
make.names / Make syntactically valid names out of character vectors.
make.unique / Makes the elements of a character vector unique by appending sequence numbers to duplicates. Useful for creating unique names of variables or factor levels.
match.call / match.call returns a call in which all of the arguments are specified by their names. The most common use is to get the call of the current function, with all arguments named, i.e., use 'match.call' if you want to return the current values of the parameters that were input to a function.
merge / Analogous to the SPSS command, JOIN MATCH / TABLE.
Combines input dataframes on common values for columns with identical names or user-specified columns.
mvrnorm / MASS package: Generates multivariate normal random variables.
mvtnorm / 'mvtnorm' package generate probabilities under a multivariate normal density function. Can be used to generate data with a given variance/covariance matrix (use 'rmvnorm'). Also, look at 'mvrnorm' in the MASS package.
n.gt.1 / JM function that counts the number of cases in each condition (combination of factor levels) in a multifactor between subjects anova. The output is TRUE if the condition has more than 1 observation and FALSE if it has 0 or 1 observation. This function is useful in combination with 'studres' in the MASS library because this function sometimes yields a value of 0 where it should be NA when a cell has only 1 observation.
na.omit / Omits cases from a dataframe if any variables have missing data.
onecode / JM function that creates a oneway anova factor from multiple input factors. 'onecode' is useful whenever it is easer to treat a multifactor between subects anova as a oneway anova.
oneway.test / Computes a oneway anova (between subjects). Default is to NOT assume homogeneity of variance.
optimize / The functionoptimizesearches the interval fromlowertoupperfor a minimum or maximum of the functionfwith respect to its first argument.
p.adjust / Multiple comparison procedure: Given a set of p values, returns p values adjusted using Holm method (default), Hochberg method, or Bonferroni method (in the base package).
'package functions'
update.packages
available.packages
old.packages
new.packages
download.packages
install.packages
contrib.url / These functions can be used to automatically compare the version numbers of installed packages with the newest available version on the repositories and update outdated packages on the fly.
package.skeleton / 'package.skeleton' automates some of the setup for a new source package. It creates directories, saves functions and data to appropriate places, and creates skeleton help files and ‘README’ files describing further steps in packaging.
pairs() / Produces all pairs of scatter plots.
pairwise.prop.test
pairwise.t.test
pairwise.table
pairwise.wilcox.test / Multiple comparison procedures for various tests (in the ctest package).
par("mai") / A numerical vector of the form c(bottom, left, top, right) which gives the margin size specified in inches. (Use 'mai' or 'mar' but not both.)
par("mar") / A numerical vector of the form 'c(bottom, left, top, right)' which gives the number of lines of margin to be specified on the four sides of the plot. The default is 'c(5, 4, 4, 2) + 0.1'.
par("mex") / 'mex' is a character size expansion factor which is used to describe coordinates in the margins of plots. Note that this does not change the font size, rather specifies the size of font used to convert between 'mar' and 'mai', and between 'oma' and 'omi'. (Note that JM has been misusing this parameter until 8/21/2005.)
par("usr") / Gives the min and max on the x and y axis for the current plot.
par("xpd") / Logical. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. Can be used to put legend (or any other text) outside of the plotting region.
permn / In 'combinat' package. Generates all permutations of the elements of x, in a minimal- change order. If x is a positive integer, returns all permutations of the elements of seq(x). If argument "fun" is not null, applies a function given by the argument to each point. "..." are passed unchanged to the function given by argument fun, if any.
plot / Create plots. See Section 2RefTo: s0001 for more info.
power.prop.test / Computes power of test for equality of proportions, or determines parameters to obtain target power.
power.t.test / Compute power of test, or determine parameters to obtain target power.
proc.time / proc.time determines how much time (in seconds) the currently running R process already consumed. Value: A numeric vector of length 5, containing the user, system, and total elapsed times for the currently running R process, and the cumulative sum of user and system times of any child processes spawned by it. See also 'system.time' and 'gc'.