R Reference Card Page 2

ADVERTISEMENT

unique(x) if x is a vector or a data frame, returns a similar object but with
fft(x) Fast Fourier Transform of an array
nchar(x) number of characters
the duplicate elements suppressed
mvfft(x) FFT of each column of a matrix
Dates and Times
table(x) returns a table with the numbers of the differents values of x
filter(x,filter) applies linear filtering to a univariate time series or
The class Date has dates without times. POSIXct has dates and times, includ-
(typically for integers or factors)
to each series separately of a multivariate time series
ing time zones. Comparisons (e.g.
), seq(), and difftime() are useful.
subset(x, ...) returns a selection of x with respect to criteria (...,
Many math functions have a logical parameter na.rm=FALSE to specify miss-
Date also allows
and . ?DateTimeClasses gives more information. See
typically comparisons: x$V1 < 10); if x is a data frame, the option
ing data (NA) removal.
also package chron.
select gives the variables to be kept or dropped using a minus sign
Matrices
as.Date(s) and as.POSIXct(s) convert to the respective class;
sample(x, size) resample randomly and without replacement size ele-
t(x) transpose
format(dt) converts to a string representation. The default string
ments in the vector x, the option replace = TRUE allows to resample
diag(x) diagonal
format is “2001-02-21”. These accept a second argument to specify a
with replacement
% * % matrix multiplication
format for conversion. Some common formats are:
prop.table(x,margin=) table entries as fraction of marginal table
solve(a,b) solves a %*% x = b for x
Math
solve(a) matrix inverse of a
%a, %A Abbreviated and full weekday name.
sin,cos,tan,asin,acos,atan,atan2,log,log10,exp
rowsum(x) sum of rows for a matrix-like object; rowSums(x) is a faster
%b, %B Abbreviated and full month name.
max(x) maximum of the elements of x
version
%d Day of the month (01–31).
min(x) minimum of the elements of x
colsum(x), colSums(x) id. for columns
%H Hours (00–23).
range(x) id. then c(min(x), max(x))
rowMeans(x) fast version of row means
%I Hours (01–12).
sum(x) sum of the elements of x
colMeans(x) id. for columns
%j Day of year (001–366).
diff(x) lagged and iterated differences of vector x
Advanced data processing
%m Month (01–12).
prod(x) product of the elements of x
%M Minute (00–59).
apply(X,INDEX,FUN=) a vector or array or list of values obtained by
mean(x) mean of the elements of x
%p AM/PM indicator.
applying a function FUN to margins (INDEX) of X
median(x) median of the elements of x
%S Second as decimal number (00–61).
lapply(X,FUN) apply FUN to each element of the list X
quantile(x,probs=) sample quantiles corresponding to the given prob-
%U Week (00–53); the first Sunday as day 1 of week 1.
tapply(X,INDEX,FUN=) apply FUN to each cell of a ragged array given
abilities (defaults to 0,.25,.5,.75,1)
%w Weekday (0–6, Sunday is 0).
by X with indexes INDEX
weighted.mean(x, w) mean of x with weights w
%W Week (00–53); the first Monday as day 1 of week 1.
by(data,INDEX,FUN) apply FUN to data frame data subsetted by INDEX
rank(x) ranks of the elements of x
%y Year without century (00–99). Don’t use.
merge(a,b) merge two data frames by common columns or row names
var(x) or cov(x) variance of the elements of x (calculated on n 1); if x is
%Y Year with century.
xtabs(a b,data=x) a contingency table from cross-classifying factors
a matrix or a data frame, the variance-covariance matrix is calculated
%z (output only.) Offset from Greenwich; -0800 is 8 hours west of.
aggregate(x,by,FUN) splits the data frame x into subsets, computes
sd(x) standard deviation of x
%Z (output only.) Time zone as a character string (empty if not available).
summary statistics for each, and returns the result in a convenient
cor(x) correlation matrix of x if it is a matrix or a data frame (1 if x is a
form; by is a list of grouping elements, each as long as the variables
vector)
Where leading zeros are shown they will be used on output but are optional
in x
var(x, y) or cov(x, y) covariance between x and y, or between the
on input. See ?strftime.
stack(x, ...) transform data available as separate columns in a data
columns of x and those of y if they are matrices or data frames
frame or list into a single column
cor(x, y) linear correlation between x and y, or correlation matrix if they
unstack(x, ...) inverse of stack()
are matrices or data frames
reshape(x, ...) reshapes a data frame between ’wide’ format with
round(x, n) rounds the elements of x to n decimals
Plotting
repeated measurements in separate columns of the same record and
log(x, base) computes the logarithm of x with base base
plot(x) plot of the values of x (on the y-axis) ordered on the x-axis
’long’ format with the repeated measurements in separate records;
scale(x) if x is a matrix, centers and reduces the data; to center only use
plot(x, y) bivariate plot of x (on the x-axis) and y (on the y-axis)
use (direction=”wide”) or (direction=”long”)
the option center=FALSE, to reduce only scale=FALSE (by default
hist(x) histogram of the frequencies of x
Strings
center=TRUE, scale=TRUE)
barplot(x) histogram of the values of x; use horiz=FALSE for horizontal
pmin(x,y,...) a vector which ith element is the minimum of x[i],
paste(...) concatenate vectors after converting to character; sep= is the
bars
y[i], . . .
string to separate terms (a single space is the default); collapse= is
dotchart(x) if x is a data frame, plots a Cleveland dot plot (stacked plots
pmax(x,y,...) id. for the maximum
an optional string to separate “collapsed” results
line-by-line and column-by-column)
cumsum(x) a vector which ith element is the sum from x[1] to x[i]
substr(x,start,stop) substrings in a character vector; can also as-
pie(x) circular pie-chart
cumprod(x) id. for the product
sign, as substr(x, start, stop) <- value
boxplot(x) “box-and-whiskers” plot
cummin(x) id. for the minimum
strsplit(x,split) split x according to the substring split
sunflowerplot(x, y) id. than plot() but the points with similar coor-
cummax(x) id. for the maximum
grep(pattern,x) searches for matches to pattern within x; see ?regex
dinates are drawn as flowers which petal number represents the num-
union(x,y), intersect(x,y), setdiff(x,y), setequal(x,y),
gsub(pattern,replacement,x) replacement of matches determined
ber of points
is.element(el,set) “set” functions
by regular expression matching sub() is the same but only replaces
stripplot(x) plot of the values of x on a line (an alternative to
Re(x) real part of a complex number
the first occurrence.
boxplot() for small sample sizes)
Im(x) imaginary part
tolower(x) convert to lowercase
z) bivariate plot of x and y for each value or interval of
coplot(x˜y
Mod(x) modulus; abs(x) is the same
toupper(x) convert to uppercase
values of z
Arg(x) angle in radians of the complex number
match(x,table) a vector of the positions of first matches for the elements
interaction.plot (f1, f2, y) if f1 and f2 are factors, plots the
Conj(x) complex conjugate
of x among table
means of y (on the y-axis) with respect to the values of f1 (on the
convolve(x,y) compute the several kinds of convolutions of two se-
x %in% table id. but returns a logical vector
x-axis) and of f2 (different curves); the option fun allows to choose
quences
pmatch(x,table) partial matches for the elements of x among table
the summary statistic of y (by default fun=mean)

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4