├── .gitignore ├── Makefile ├── R-refcard.pdf ├── R-refcard.tex ├── README.org ├── Rpad-refcard.tex ├── makePchPdf.R ├── pch_symbol.pdf └── upload_pdf.lftp /.gitignore: -------------------------------------------------------------------------------- 1 | auto/* 2 | *.aux 3 | *.pdf 4 | *.log 5 | /R-refcard.out 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # set latexfile to the name of the main file without the .tex 2 | latexfile = R-refcard 3 | 4 | TEX = pdflatex 5 | 6 | $(latexfile).pdf : clean 7 | while ($(TEX) $(latexfile) ; \ 8 | grep -q "Rerun to get cross" $(latexfile).log ) do true ; \ 9 | done 10 | 11 | pdf : $(latexfile).pdf 12 | 13 | 14 | view : $(latexfile).pdf 15 | acroread $(latexfile).pdf 16 | 17 | clean : 18 | rm -f $(latexfile).log 19 | rm -f $(latexfile).out 20 | rm -f $(latexfile).aux 21 | rm -f $(latexfile).bbl 22 | rm -f $(latexfile).blg 23 | rm -f $(latexfile)-blx.bib 24 | rm -f $(latexfile).toc 25 | rm -f *.*~ 26 | 27 | purge : clean 28 | rm $(latexfile).pdf 29 | 30 | spellcheck: 31 | hunspell -l -t -i utf-8 $(latexfile).tex 32 | 33 | test: 34 | chktex $(latexfile).tex 35 | 36 | upload: 37 | lftp -f upload_pdf.lftp 38 | echo "Now send mail to CRAN@R-project.org subject: CRAN submission R-refcard.pdf" 39 | -------------------------------------------------------------------------------- /R-refcard.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonasstein/R-Reference-Card/86f864845183f1228701e23167e421f2cd16ef2d/R-refcard.pdf -------------------------------------------------------------------------------- /R-refcard.tex: -------------------------------------------------------------------------------- 1 | \documentclass[8pt,landscape]{article} 2 | %\usepackage{pslatex} 3 | \usepackage{graphicx} 4 | \usepackage{multicol} 5 | \usepackage{calc} 6 | \usepackage{color} 7 | 8 | \usepackage{hyperref} 9 | \usepackage[]{url} 10 | \hypersetup{% 11 | colorlinks=true, 12 | linkcolor = black, 13 | citecolor = black, 14 | urlcolor = blue, 15 | pdfpagemode=UseNone, 16 | pdfstartview=FitH, 17 | pdfauthor= {Jonas Stein, Tom Short, Emanuel Paradis}, 18 | pdftitle= R Reference Card 19 | } 20 | 21 | \urlstyle{same} 22 | 23 | 24 | % Turn off header and footer 25 | \pagestyle{empty} 26 | 27 | %\setlength{\leftmargin}{0.75in} 28 | \setlength{\oddsidemargin}{-0.75in} 29 | \setlength{\evensidemargin}{-0.75in} 30 | \setlength{\textwidth}{10.5in} 31 | 32 | 33 | \setlength{\topmargin}{-0.2in} 34 | \setlength{\textheight}{7.4in} 35 | \setlength{\headheight}{0in} 36 | \setlength{\headsep}{0in} 37 | 38 | \pdfpageheight\paperheight 39 | \pdfpagewidth\paperwidth 40 | 41 | % Redefine section commands to use less space 42 | \makeatletter 43 | \renewcommand\section{\@startsection{section}{1}{0mm}% 44 | {-24pt}% \@plus -12pt \@minus -6pt}% 45 | {0.5ex}% 46 | {\color{blue}\normalfont\large\bfseries}} 47 | \makeatother 48 | 49 | % Don't print section numbers 50 | \setcounter{secnumdepth}{0} 51 | 52 | \setlength{\parindent}{0pt} 53 | \setlength{\parskip}{0pt} 54 | 55 | \newcommand{\code}{\texttt} 56 | \newcommand{\bcode}[1]{\texttt{\textbf{#1}}} 57 | \newcommand\F{\code{FALSE}} 58 | \newcommand\T{\code{TRUE}} 59 | 60 | %\newcommand{\hangpara}[2]{\hangindent#1\hangafter#2\noindent} 61 | %\newenvironment{hangparas}[2]{\setlength{\parindent}{\z@}\everypar={\hangpara{#1}{#2}}} 62 | 63 | \newcommand{\describe}[1]{\begin{description}{#1}\end{description}} 64 | 65 | 66 | % ----------------------------------------------------------------------- 67 | 68 | \begin{document} 69 | 70 | %\raggedright 71 | \footnotesize 72 | \begin{multicols*}{3} 73 | 74 | % multicol parameters 75 | % These lengths are set only within the two main columns 76 | %\setlength{\columnseprule}{0.25pt} 77 | \setlength{\premulticols}{1pt} 78 | \setlength{\postmulticols}{1pt} 79 | \setlength{\multicolsep}{1pt} 80 | \setlength{\columnsep}{2pt} 81 | 82 | \begin{center} 83 | {\Large{\textbf{\color{blue}R Reference Card}}} \\ 84 | \today 85 | \end{center} 86 | \href{mailto:news@jonasstein.de}{Jonas Stein} maintains 87 | the source since 2012 on \href{https://github.com/jonasstein/R-Reference-Card}{github} feel free to contact him for contributions. 88 | Parent versions by Tom Short, includes material from 89 | \href{http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf}{R for Beginners} 90 | by courtesy of Emmanuel Paradis.\\ 91 | 92 | Examples in this document use the variables \code{df} = data frame object, \code{v} = vector, \code{s} = string, \code{f} = filename as string all others are not yet rewritten. 93 | 94 | \section{Getting help} 95 | \everypar={\hangindent=9mm} 96 | 97 | \bcode{help(topic)} documentation on \code{topic} 98 | 99 | \bcode{?topic} short alternative to \code{help} 100 | 101 | \bcode{?.Machine} help about maximum values on your machine 102 | 103 | \bcode{help.search("topic")} search the help system 104 | 105 | \bcode{apropos("topic")} the names of all objects in the search list 106 | matching the regular expression "topic" 107 | 108 | \bcode{help.start()} start the HTML version of help 109 | 110 | \bcode{str(a)} display the internal structure of an R object 111 | 112 | \bcode{summary(a)} gives a ``summary'' of \code{a}, usually a 113 | statistical summary but it is \emph{generic} meaning it has different operations for different 114 | classes of \code{a} 115 | 116 | \bcode{ls()} show objects in the search path; specify \code{pat="pat"} 117 | to search on a pattern 118 | 119 | \bcode{ls.str()} str() for each variable in the search path 120 | 121 | \bcode{dir()} show files in the current directory 122 | 123 | \bcode{methods(a)} shows S3 methods of \code{a} 124 | 125 | \bcode{methods(class=class(a))} lists all the methods to handle objects 126 | of class a 127 | 128 | 129 | 130 | 131 | \section{Input and output} 132 | \everypar={\hangindent=9mm} 133 | 134 | \bcode{source("my.R")} includes and executes my.R in this place 135 | 136 | \bcode{data(f)} loads specified data sets 137 | 138 | \bcode{library(s)} load add-on packages 139 | 140 | \bcode{read.table(f)} reads a file in table format and 141 | creates a data frame from it; the default separator 142 | \code{sep=""} is any whitespace; use \code{header=TRUE} 143 | to read the first line as a header of column names; use \code{as.is=TRUE} to 144 | prevent character vectors from being converted to 145 | factors; use \code{comment.char=""} to prevent 146 | \code{"\#"} from being interpreted as a comment; use 147 | \code{skip=n} to skip \code{n} lines before reading data; see 148 | the help for options on row naming, NA treatment, and 149 | others 150 | 151 | \bcode{read.csv(f,header=TRUE)} id. but with defaults set for reading 152 | comma-delimited files 153 | 154 | \bcode{read.delim(f",header=TRUE)} id. but with defaults set for reading 155 | tab-delimited files 156 | 157 | \bcode{read.fwf(f,widths,header=FALSE,sep="\t",as.is=FALSE)} read a table of \emph{f}ixed \emph{w}idth \emph{f}ormatted data into a 158 | 'data.frame'; \code{widths} is an integer vector, giving the widths of the fixed-width fields 159 | 160 | 161 | \bcode{save(f,...)} saves the specified objects (...) in the XDR 162 | platform-independent binary format 163 | 164 | \bcode{save.image(f)} saves all objects 165 | 166 | \bcode{load(f)} load the datasets written with \code{save} 167 | 168 | 169 | \bcode{cat(..., file=f, sep=" ")} prints the arguments after coercing to 170 | character; \code{sep} is the character separator between arguments 171 | 172 | \bcode{print(a, ...)} prints its arguments; generic, meaning it can 173 | have different methods for different objects 174 | 175 | \bcode{format(x,...)} format an R object for pretty printing 176 | 177 | \bcode{write.table(x, file=f,row.names=TRUE, col.names=TRUE, sep=" ")} 178 | prints \code{x} after converting to a data frame; if \code{quote} is 179 | \code{TRUE}, character or factor columns are 180 | surrounded by quotes (\code{"}); \code{sep} is the field 181 | separator; \code{eol} is the end-of-line separator; 182 | \code{na} is the string for missing values; use 183 | \code{col.names=NA} to add a blank column header to 184 | get the column headers aligned correctly for 185 | spreadsheet input 186 | 187 | \bcode{sink(f)} output to \code{f}, until \code{sink()} 188 | 189 | \everypar={\hangindent=0mm} 190 | Most of the I/O functions have a \code{file} argument and 191 | means the standard input or output. Connections can include files, pipes, zipped 192 | files, clipboard and R variables.\\ 193 | 194 | To exchange tables with office applications on windows via clipboard, use \\ 195 | \code{df <- read.delim("clipboard")}\\ 196 | \code{write.table(df,"clipboard",sep="\textbackslash t",col.names=NA)} 197 | 198 | For database interaction, see packages \code{RODBC}, \code{DBI}, 199 | \code{RMySQL}, \code{RPgSQL}, \code{ROracle}, for other file formats see 200 | \code{XML}, \code{hdf5}, \code{netCDF} 201 | 202 | 203 | 204 | 205 | \section{Data creation} 206 | \everypar={\hangindent=9mm} 207 | 208 | \bcode{c(...)} generic function to combine arguments with the default 209 | forming a vector; 210 | with \code{recursive=TRUE} descends through lists combining all elements 211 | into one vector 212 | 213 | \bcode{from:to} generates a sequence; ``:'' has operator priority; \code{1:4 214 | + 1} returns \code{[1] 2 3 4 5} 215 | 216 | \bcode{seq(from,to)} generates a sequence 217 | \code{by=} specifies increment; \code{length=} specifies desired length 218 | 219 | \bcode{seq(along=x)} generates \code{1, 2, ..., length(along)}; useful for 220 | \code{for} loops 221 | 222 | \bcode{rep(x,times)} replicate \code{x} \code{times}; use \code{each=} 223 | to repeat ``each'' element of \code{x} \code{each} times; 224 | \code{rep(c(1,2,3),2)} is 1 2 3 1 2 3; \code{rep(c(1,2,3),each=2)} is 1 1 2 2 3 3 225 | 226 | \bcode{data.frame(...)} create a data frame of the named or unnamed arguments; 227 | \code{data.frame(v=1:4,ch=c("a","B","c","d"),n=10)}; shorter vectors 228 | are recycled to the length of the longest 229 | 230 | \bcode{list(...)} create a list of the named or unnamed arguments; 231 | \code{list(a=c(1,2),b="hi",c=3i)}; 232 | 233 | \bcode{array(x,dim=)} array with data \code{x}; specify 234 | dimensions like \code{dim=c(3,4,2)}; elements of \code{x} recycle if \code{x} 235 | is not long enough 236 | 237 | \bcode{matrix(x,nrow=,ncol=)} matrix; elements of \code{x} recycle 238 | 239 | \bcode{factor(x,levels=)} encodes a vector \code{x} as a factor 240 | 241 | \bcode{gl(n,k,length=n*k,labels=1:n)} generate levels (factors) by specifying 242 | the pattern of their levels; \code{k} is the number of levels, and \code{n} is the 243 | number of replications 244 | 245 | \bcode{expand.grid()} a data frame from all combinations of the supplied vectors 246 | or factors 247 | 248 | \bcode{cbind(df1, df2), rbind(df1,df2)} combine arguments by columns (rows) for data frames and the like 249 | 250 | 251 | 252 | \section{Data destruction} 253 | \bcode{rm(myvar)} removes object \code{myvar} from memory 254 | 255 | \bcode{rm(list = ls(all = TRUE))} removes all objects from memory 256 | 257 | \section{addressing vectors} 258 | 259 | \begin{tabular}{@{}l@{\ }l} 260 | \code{v[n]} & \code{n}$^{th}$ element\\ 261 | \code{v[-n]} & all \emph{but} the \code{n}$^{th}$ element\\ 262 | \code{v[1:n]} & first \code{n} elements\\ 263 | \code{v[-(1:n)]} & elements from \code{n+1} to the end\\ 264 | \code{v[c(1,4,2)]} & specific elements\\ 265 | \code{v["name"]} & element named \code{"name"}\\ 266 | \code{v[x > 3]} & all elements greater than 3\\ 267 | \code{v[x > 3 \& x < 5]} & all elements between 3 and 5\\ 268 | \code{v[x \%in\% c("a","and","the")]} & elements in the given set\\ 269 | \end{tabular} 270 | 271 | \section{addressing lists} 272 | \samepage\begin{tabular}{@{}l@{\ }l} 273 | \code{x[n]} & list with elements \code{n}\\ 274 | \code{x[[n]]} & \code{n}$^{th}$ element of the list\\ 275 | \code{x[["name"]]} & element of the list named \code{"name"}\\ 276 | \code{x\$name} & id.\\ 277 | \end{tabular} 278 | 279 | \section{addressing matrices} 280 | \begin{tabular}{@{}l@{\ }l} 281 | \code{x[i,j]} & element at row \code{i}, column \code{j}\\ 282 | \code{x[i,]} & row \code{i}\\ 283 | \code{x[,j]} & column \code{j}\\ 284 | \code{x[,c(1,3)]} & columns 1 and 3\\ 285 | \code{x["name",]} & row named \code{"name"}\\ 286 | \end{tabular} 287 | 288 | Indexing data frames (matrix indexing plus the following) 289 | 290 | \begin{tabular}{@{}l@{\ }l} 291 | \code{df[["name"]]} & column named \code{"name"}\\ 292 | \code{df\$name} & id.\\ 293 | \end{tabular} 294 | 295 | 296 | 297 | \section{Variable conversion} 298 | \everypar={\hangindent=9mm} 299 | 300 | \bcode{as.array(x), as.data.frame(x), as.numeric(x), as.logical(x), 301 | as.complex(x), as.character(x), ...} convert type; \code{methods(as)} 302 | shows a complete list 303 | 304 | 305 | 306 | \section{Variable information} 307 | \everypar={\hangindent=9mm} 308 | 309 | \bcode{is.na(x), is.null(x), is.array(x), is.data.frame(x), is.numeric(x), 310 | is.complex(x), is.character(x), ...} test for type; for a complete list, 311 | use \code{methods(is)} 312 | 313 | \bcode{length(x)} number of elements in \code{x} 314 | 315 | \bcode{dim(x)} Retrieve or set the dimension of an object; 316 | \code{dim(x) <- c(3,2)} 317 | 318 | \bcode{dimnames(x)} Retrieve or set the dimension names of an object 319 | 320 | \bcode{nrow(x)} number of rows; \code{NROW(x)} is the same but treats 321 | a vector as a one-row matrix 322 | 323 | \bcode{ncol(x)} and \bcode{NCOL(x)} id. for columns 324 | 325 | \bcode{class(x)} get or set the class of \code{x}; \code{class(x) <- "myclass"} 326 | 327 | \bcode{unclass(x)} remove the class attribute of \code{x} 328 | 329 | \bcode{attr(x,which)} get or set the attribute \code{which} of \code{x} 330 | 331 | \bcode{attributes(obj)} get or set the list of attributes of \code{obj} 332 | 333 | 334 | 335 | 336 | 337 | \section{Data selection and manipulation} 338 | \everypar={\hangindent=9mm} 339 | 340 | \bcode{which.max(v), which.min(v)} returns the index of the maximum (minimum) element of 341 | \bcode{v} 342 | 343 | \bcode{rev(v)} reverses the elements of \code{v} 344 | 345 | \bcode{sort(v)} sorts the elements of \code{v} in increasing order; to sort in decreasing order: \code{rev(sort(x))} 346 | 347 | \bcode{cut(x,breaks)} divides \code{x} into intervals (factors); 348 | \code{breaks} is the number of cut intervals or a vector of cut points 349 | 350 | \bcode{match(x, y)} returns a vector of the same length than \code{x} with the elements of \code{x} which are in \code{y} (\code{NA} otherwise) 351 | 352 | \bcode{which(x == a)} returns a vector of the indices of \code{x} if the comparison operation is true (\T), in this example the values of \code{i} for which \code{x[i] == a} (the argument of this function must be a variable of mode logical) 353 | 354 | \bcode{na.omit(x)} suppresses the observations with missing data (\code{NA}) (suppresses the corresponding line if \code{x} is a matrix or a data frame) 355 | 356 | \bcode{na.fail(x)} returns an error message if \code{x} contains at least one \code{NA} 357 | 358 | \bcode{unique(x)} if \code{x} is a vector or a data frame, returns a similar object but with the duplicate elements suppressed 359 | 360 | \bcode{table(x)} returns a table with the numbers of the differents values of \code{x} (typically for integers or factors) 361 | 362 | \bcode{subset(x, ...)} returns a selection of \code{x} with respect to criteria (\code{...}, typically comparisons: \code{x\$V1 < 10}); if \code{x} is a data frame, the option \code{select} gives the variables to be kept or dropped using a minus sign 363 | 364 | \bcode{sample(x, size)} resample randomly and without replacement \code{size} elements in the vector \code{x}, the option \code{replace = TRUE} allows to resample with replacement 365 | 366 | \bcode{prop.table(x,margin=)} table entries as fraction of marginal table 367 | 368 | 369 | 370 | 371 | \section{Math} 372 | \everypar={\hangindent=9mm} 373 | 374 | \bcode{sin,cos,tan,asin,acos,atan,atan2,log,log10,exp} 375 | 376 | \bcode{range(x)} id. then \code{c(min(x), max(x))} 377 | 378 | \bcode{sum(x)} sum of the elements of \code{x} 379 | 380 | \bcode{diff(x)} lagged and iterated differences of vector \code{x} 381 | 382 | \bcode{prod(x)} product of the elements of \code{x} 383 | 384 | \bcode{mean(x)} mean of the elements of \code{x} 385 | 386 | \bcode{median(x)} median of the elements of \code{x} 387 | 388 | \bcode{quantile(x,probs=)} sample quantiles 389 | corresponding to the given probabilities (defaults to 0,.25,.5,.75,1) 390 | 391 | \bcode{weighted.mean(x, w)} mean of \code{x} with weights \code{w} 392 | 393 | \bcode{rank(x)} ranks of the elements of \code{x} 394 | 395 | \bcode{var(x)} or \code{cov(x)} variance of the elements of \code{x} 396 | (calculated on $n-1$); if \code{x} is a matrix or a data frame, the 397 | variance-covariance matrix is calculated 398 | 399 | \bcode{sd(x)} standard deviation of \code{x} 400 | 401 | \bcode{cor(x)} correlation matrix of \code{x} if it is a matrix or a 402 | data frame (1 if \code{x} is a vector) 403 | 404 | \bcode{var(x, y)} or \code{cov(x, y)} covariance between \code{x} and \code{y}, or between the columns of \code{x} and those of \code{y} if they are matrices or data frames 405 | 406 | \bcode{cor(x, y)} linear correlation between \code{x} and \code{y}, or correlation matrix if they are matrices or data frames 407 | 408 | \bcode{choose(n, k)} computes the combinations of $k$ events among $n$ repetitions = $n!/[(n-k)!k!]$ 409 | 410 | \bcode{round(x, n)} rounds the elements of \code{x} to \code{n} 411 | decimals 412 | 413 | \bcode{log(v, base)} computes the logarithm of \code{x} with base \code{base} 414 | \bcode{log10(v)} \code{base} =10 415 | 416 | \bcode{scale(x)} if \code{x} is a matrix, centers and reduces the data; to center only use the option \code{center=FALSE}, to reduce only \code{scale=FALSE} (by default \code{center=TRUE, scale=TRUE}) 417 | 418 | \bcode{pmin(x,y,...)} a vector which $i$th element is the minimum of \code{x[i]}, \code{y[i]}, \ldots 419 | 420 | \bcode{pmax(x,y,...)} id. for the maximum 421 | 422 | \bcode{cumsum(v)} a vector which $i$th element is the sum from \code{x[1]} to \code{x[i]} 423 | 424 | \bcode{cumprod(v)} $f_i = \Pi_{j=1..i} x_j = (x_1, x_1 \cdot x_2, \dots)$ 425 | 426 | \bcode{cummin(v)} $f_i = \min(x_1 \dots x_i)$ 427 | 428 | \bcode{cummax(v)} id. for the maximum 429 | 430 | \bcode{union(x,y)},~\bcode{intersect(x,y)},~\bcode{setdiff(x,y)},~\bcode{setequal(x,y)}, \bcode{is.element(el,set)} ``set'' functions 431 | 432 | \bcode{Re(z), Im(z)} real and imaginary part of a complex number 433 | 434 | \bcode{Mod(z)} modulus; \code{abs(x)} is the same 435 | 436 | \bcode{Arg(z)} angle in radians of the complex number 437 | 438 | \bcode{Conj(z)} complex conjugate 439 | 440 | \bcode{convolve(x,y)} compute the several kinds of 441 | convolutions of two sequences 442 | 443 | \bcode{fft(v)} Fast Fourier Transform 444 | \bcode{mvfft(x)} FFT of each column of a matrix 445 | 446 | \bcode{filter(x,filter)} applies linear filtering to a univariate time series or to each 447 | series separately of a multivariate time series 448 | 449 | \everypar={\hangindent=0mm} 450 | Many math functions have a logical parameter \code{na.rm=FALSE} to 451 | specify missing data (NA) removal. 452 | 453 | 454 | 455 | 456 | \section{Matrices} 457 | \everypar={\hangindent=9mm} 458 | 459 | \bcode{t(x)} transpose 460 | 461 | \bcode{diag(x)} diagonal 462 | 463 | \bcode{\%*\%} matrix multiplication and scalar product 464 | 465 | \bcode{solve(a,b)} solves \code{a \%*\% x = b} for \code{x} 466 | 467 | \bcode{solve(a)} matrix inverse of \code{a} 468 | 469 | \bcode{rowsum(x)} sum of rows for a matrix-like object; 470 | \bcode{rowSums(x)} is a faster version 471 | 472 | \bcode{colsum(x)}, \bcode{colSums(x)} id. for columns 473 | 474 | \bcode{rowMeans(x)} fast version of row means 475 | \bcode{colMeans(x)} id. for columns 476 | 477 | 478 | \section{Advanced data processing} 479 | \everypar={\hangindent=9mm} 480 | 481 | \bcode{apply(X,INDEX,FUN=)} a vector or array or list of values obtained by applying a 482 | function \code{FUN} to margins (\code{INDEX}) of \code{X} 483 | 484 | \bcode{lapply(X,FUN)} apply \code{FUN} to each element of the list \code{X} 485 | 486 | \bcode{tapply(X,INDEX,FUN=)} apply \code{FUN} to each cell 487 | of a ragged array given by \code{X} with indexes \code{INDEX} 488 | 489 | \bcode{by(data,INDEX,FUN)} apply \code{FUN} to data frame \code{data} 490 | subsetted by \code{INDEX} 491 | 492 | \bcode{merge(a,b)} merge two data frames by common columns or row names 493 | 494 | \bcode{xtabs(a~b,data=x)} a contingency table from cross-classifying factors 495 | 496 | \bcode{aggregate(df,by,FUN)} splits \code{df} into subsets, computes summary statistics for 497 | each, and returns the result in a convenient form; \code{by} is a 498 | list of grouping elements, each as long as the variables in \code{df} 499 | 500 | \bcode{stack(x, ...)} transform data available as 501 | separate columns in a data frame or list into a single column 502 | 503 | \bcode{unstack(x, ...)} inverse of \code{stack()} 504 | 505 | \bcode{reshape(x, ...)} reshapes a data frame between 'wide' format with 506 | repeated measurements in separate columns of the same record and 507 | 'long' format with the repeated measurements in separate records; 508 | use \code(direction="wide") or \code(direction="long") 509 | 510 | 511 | 512 | 513 | 514 | \section{Strings} 515 | \everypar={\hangindent=9mm} 516 | 517 | \bcode{paste(s1,s2, sep=" ")} concatenate vectors after converting to character; 518 | \code{collapse=} is an optional string to separate ``collapsed'' results 519 | \bcode{paste0(s1,s2)} paste without seperator (since R 2.15) 520 | 521 | \bcode{substr(s,start,stop)} substrings in a character vector; can also assign, as \code{substr(s, start, stop) <- value} 522 | 523 | \bcode{strsplit(s,split)} split \code{s} according to the substring \code{split} 524 | 525 | \bcode{grep(pattern,s)} search \code{pattern} in \code{s}; see \code{?regex} 526 | 527 | \bcode{gsub(pattern,replacement,x)} replacement of matches determined by 528 | regular expression matching \code{sub()} is the same but only 529 | replaces the first occurrence. 530 | 531 | \bcode{tolower(s), toupper(s)} convert to lowercase (uppercase) 532 | 533 | \bcode{match(x,table)} a vector of the positions of first matches for the elements of \code{x} among \code{table} 534 | 535 | \bcode{x \%in\% table} id. but returns a logical vector 536 | 537 | \bcode{pmatch(x,table)} partial matches for the elements of \code{x} among \code{table} 538 | 539 | \bcode{nchar(s)} number of characters 540 | 541 | 542 | \section{\color{blue}Dates and Times} 543 | 544 | The class \code{Date} has dates without times. \code{POSIXct} has 545 | dates and times, including time zones. Comparisons (e.g. $>$), 546 | \code{seq()}, and \code{difftime()} are useful. \code{Date} also allows 547 | $+$ and $-$. \code{?DateTimeClasses} gives more information. See also package 548 | \code{chron}. 549 | 550 | \everypar={\hangindent=9mm} 551 | 552 | \bcode{as.Date(s)} and \bcode{as.POSIXct(s)} convert to the respective 553 | class; \code{format(dt)} converts to a string representation. The 554 | default string format is ``2012-02-21''. These accept a second argument 555 | to specify a format for conversion. Some common formats are: 556 | 557 | \describe{ 558 | \itemsep=0pt\parskip=0pt 559 | \item{\code{\%a}, \code{\%A}} {Abbreviated and full weekday name.} 560 | \item{\code{\%b}, \code{\%B}} {Abbreviated and full month name.} 561 | \item{\code{\%d}} {Day of the month (01--31).} 562 | \item{\code{\%H}} {Hours (00--23).} 563 | \item{\code{\%I}} {Hours (01--12).} 564 | \item{\code{\%j}} {Day of year (001--366).} 565 | \item{\code{\%m}} {Month (01--12).} 566 | \item{\code{\%M}} {Minute (00--59).} 567 | \item{\code{\%p}} {AM/PM indicator. } 568 | \item{\code{\%S}} {Second as decimal number (00--61).} 569 | \item{\code{\%U}} {Week (00--53); the first Sunday as day 1 of week 1.} 570 | \item{\code{\%w}} {Weekday (0--6, Sunday is 0).} 571 | \item{\code{\%W}} {Week (00--53); the first Monday as day 1 of week 1.} 572 | \item{\code{\%y}} {Year without century (00--99). Avoid it.} 573 | \item{\code{\%Y}} {Year with century.} 574 | \item{\code{\%z}} {(read only) Offset from Greenwich; \code{-0800} 575 | is 8 hours west of.} 576 | \item{\code{\%Z}} {(read only) Time zone as a character string 577 | (empty if not available).} 578 | } 579 | 580 | Where leading zeros are shown they will be used on output but are 581 | optional on input. See \code{?strftime}. 582 | 583 | 584 | 585 | 586 | 587 | \section{Plotting} 588 | 589 | \everypar={\hangindent=9mm} 590 | 591 | \bcode{plot(y)} plot of the values of \code{y} (on the $y$-axis) ordered on the $x$-axis 592 | 593 | \bcode{plot(x=xv, y=yv)} bivariate plot of \code{xv} (on the $x$-axis) and \code{yv} (on the $y$-axis) 594 | 595 | \bcode{hist(x)} histogram of the frequencies of \code{x} 596 | 597 | \bcode{barplot(x)} histogram of the values of \code{x}; use 598 | \code{horiz=FALSE} for horizontal bars 599 | 600 | \bcode{dotchart(x)} if \code{x} is a data frame, plots a Cleveland dot plot (stacked plots line-by-line and column-by-column) 601 | 602 | \bcode{pie(x)} circular pie-chart 603 | 604 | \bcode{boxplot(x)} ``box-and-whiskers'' plot 605 | 606 | \bcode{sunflowerplot(x, y)} id. than \code{plot()} but the points with similar coordinates are drawn as flowers which petal number represents the number of points 607 | 608 | \bcode{stripplot(x)} plot of the values of \code{x} on a line (an alternative to \code{boxplot()} for small sample sizes) 609 | 610 | \bcode{coplot(x\~{}y $\mid$ z)} bivariate plot of \code{x} and \code{y} for each value or interval of values of \code{z} 611 | 612 | \bcode{interaction.plot (f1, f2, y)} if \code{f1} and \code{f2} are factors, plots the means of \code{y} (on the $y$-axis) with respect to the values of \code{f1} (on the $x$-axis) and of \code{f2} (different curves); the option \code{fun} allows to choose the summary statistic of \code{y} (by default \code{fun=mean}) 613 | 614 | \bcode{matplot(x,y)} bivariate plot of the first column of \code{x} {\it vs.} the first one of \code{y}, the second one of \code{x} {\it vs.} the second one of \code{y}, etc. 615 | 616 | \bcode{fourfoldplot(x)} visualizes, with quarters of circles, the association between two dichotomous variables for different populations (\code{x} must be an array with \code{dim=c(2, 2, k)}, or a matrix with \code{dim=c(2, 2)} if $k=1$) 617 | 618 | \bcode{assocplot(x)} Cohen--Friendly graph showing the deviations from independence of rows and columns in a two dimensional contingency table 619 | 620 | \bcode{mosaicplot(x)} `mosaic' graph of the residuals from a log-linear regression of a contingency table 621 | 622 | \bcode{pairs(x)} if \code{x} is a matrix or a data frame, draws all possible bivariate plots between the columns of \code{x} 623 | 624 | \bcode{plot.ts(x)} if \code{x} is an object of class \code{"ts"}, plot of \code{x} with respect to time, \code{x} may be multivariate but the series must have the same frequency and dates 625 | 626 | \bcode{ts.plot(x)} id. but if \code{x} is multivariate the series may have different dates and must have the same frequency 627 | 628 | \bcode{qqnorm(x)} quantiles of \code{x} with respect to the values expected under a normal law 629 | 630 | \bcode{qqplot(x, y)} quantiles of \code{y} with respect to the quantiles of \code{x} 631 | 632 | \bcode{contour(x, y, z)} contour plot (data are interpolated to draw the curves), \code{x} and \code{y} must be vectors and \code{z} must be a matrix so that \code{dim(z)=c(length(x), length(y))} (\code{x} and \code{y} may be omitted) 633 | 634 | \bcode{filled.contour(x, y, z)} id. but the areas between the contours are coloured, and a legend of the colours is drawn as well 635 | 636 | \bcode{image(x, y, z)} id. but with colours (actual data are plotted) 637 | 638 | \bcode{persp(x, y, z)} id. but in perspective (actual data are plotted) 639 | 640 | \bcode{stars(x)} if \code{x} is a matrix or a data frame, draws a graph with segments or a star where each row of \code{x} is represented by a star and the columns are the lengths of the segments 641 | 642 | \bcode{symbols(x, y, ...)} draws, at the coordinates given by \code{x} and \code{y}, symbols (circles, squares, rectangles, stars, thermometres or ``boxplots'') which sizes, colours \ldots{} are specified by supplementary arguments 643 | 644 | \bcode{termplot(mod.obj)} plot of the (partial) effects of a regression model (\code{mod.obj}) 645 | 646 | The following parameters are common to many plotting functions: 647 | 648 | \bcode{add=FALSE} if \T{} superposes the plot on the previous one (if it exists) 649 | 650 | \bcode{axes=TRUE} if \F{} does not draw the axes and the box 651 | 652 | \bcode{type="p"} specifies the type of plot, \code{"p"}: points, \code{"l"}: lines, \code{"b"}: points connected by lines, \code{"o"}: id. but the lines are over the points, \code{"h"}: vertical lines, \code{"s"}: steps, the data are represented by the top of the vertical lines, \code{"S"}: id. but the data are represented by the bottom of the vertical lines 653 | 654 | \bcode{xlim=, ylim=} specifies the lower and upper limits of the axes, for example with \code{xlim=c(1, 10)} or \code{xlim=range(x)} 655 | 656 | \bcode{xlab=, ylab=} annotates the axes, must be variables of mode character 657 | 658 | \bcode{main=} main title, must be a variable of mode character 659 | 660 | \bcode{sub=} sub-title (written in a smaller font) 661 | 662 | 663 | 664 | 665 | \section{Low-level plotting commands} 666 | \everypar={\hangindent=9mm} 667 | 668 | \bcode{points(x, y)} adds points (the option \code{type=} can be used) 669 | 670 | \bcode{lines(x, y)} id. but with lines 671 | 672 | \bcode{text(x, y, \mbox{labels}, ...)} adds text given by \code{labels} at coordinates (\code{x},\code{y}); a typical use is: \code{plot(x, y, type="n"); text(x, y, names)} 673 | 674 | \bcode{mtext(text, side=3, line=0, ...)} adds text given by \code{text} in the margin specified by \code{side} (see \code{axis()} below); \code{line} specifies the line from the plotting area 675 | 676 | \bcode{segments(x0, y0, x1, y1)} draws lines from points (\code{x0},\code{y0}) to points (\code{x1},\code{y1}) 677 | 678 | \bcode{arrows(x0, y0, x1, y1, angle= 30, code=2)} id. with arrows at points (\code{x0},\code{y0}) if \code{code=2}, at points (\code{x1},\code{y1}) if \code{code=1}, or both if \code{code=3}; \code{angle} controls the angle from the shaft of the arrow to the edge of the arrow head 679 | 680 | \bcode{abline(a,b)} draws a line of slope \code{b} and intercept \code{a} 681 | 682 | \bcode{abline(h=y)} draws a horizontal line at ordinate \code{y} 683 | 684 | \bcode{abline(v=x)} draws a vertical line at abcissa \code{x} 685 | 686 | \bcode{abline(lm.obj)} draws the regression line given by \code{lm.obj} 687 | 688 | \bcode{rect(x1, y1, x2, y2)} draws a rectangle which left, right, bottom, and top limits are \code{x1}, \code{x2}, \code{y1}, and \code{y2}, respectively 689 | 690 | \bcode{polygon(x, y)} draws a polygon linking the points with coordinates given by \code{x} and \code{y} 691 | 692 | \bcode{legend(x, y, legend)} adds the legend at the point (\code{x},\code{y}) with the symbols given by \code{legend} 693 | 694 | \bcode{title()} adds a title and optionally a sub-title 695 | 696 | \bcode{axis(side, vect)} adds an axis at the bottom (\code{side=1}), on the left (\code{2}), at the top (\code{3}), or on the right (\code{4}); \code{vect} (optional) gives the abcissa (or ordinates) where tick-marks are drawn 697 | 698 | \bcode{rug(v)} draws the data on the $x$-axis as small vertical lines 699 | 700 | \bcode{locator(n, type="n", ...)} returns the coordinates ($x,y$) after the user has clicked \code{n} times on the plot with the mouse; also draws symbols (\code{type="p"}) or lines (\code{type="l"}) with respect to optional graphic parameters (\code{...}); by default nothing is drawn (\code{type="n"}) 701 | 702 | 703 | 704 | 705 | \section{Graphical parameters} 706 | 707 | These can be set globally with \bcode{par(...)}; many can be passed as 708 | parameters to plotting commands. 709 | 710 | \everypar={\hangindent=9mm} 711 | 712 | \bcode{adj} controls text justification (\code{0} left-justified, \code{0.5} centred, \code{1} right-justified) 713 | 714 | \bcode{bg} specifies the colour of the background (ex. : \code{bg="red"}, \code{bg="blue"}, \ldots{} the list of the 657 available colours is displayed with \code{colors()}) 715 | 716 | \bcode{bty} controls the type of box drawn around the plot, allowed values are: \code{"o"}, \code{"l"}, \code{"7"}, \code{"c"}, \code{"u"} ou \code{"]"} (the box looks like the corresponding character); if \code{bty="n"} the box is not drawn 717 | 718 | \bcode{cex} a factor controlling the default size of texts and symbols; you can scale numbers on the axes, \code{cex.axis}, the axis labels, \code{cex.lab}, the title, \code{cex.main}, and the sub-title, \code{cex.sub} 719 | 720 | \bcode{col} controls the color of symbols and lines; use color names: 721 | \code{"red"}, \code{"blue"} see \code{colors()} or as \code{"\#RRGGBB"}; 722 | see \code{rgb()}, \code{hsv()}, \code{gray()}, and \code{rainbow()}; as for \code{cex} there are: \code{col.axis}, \code{col.lab}, \code{col.main}, \code{col.sub} 723 | 724 | \bcode{font} an integer which controls the style of text (\code{1}: normal, \code{2}: italics, \code{3}: bold, \code{4}: bold italics); as for \code{cex} there are: \code{font.axis}, \code{font.lab}, \code{font.main}, \code{font.sub} 725 | 726 | \bcode{las} an integer which controls the orientation of the axis labels (\code{0}: parallel to the axes, \code{1}: horizontal, \code{2}: perpendicular to the axes, \code{3}: vertical) 727 | 728 | \bcode{lty} controls the type of lines, can be an integer or string (\code{1}: \code{"solid"}, \code{2}: \code{"dashed"}, \code{3}: \code{"dotted"}, \code{4}: \code{"dotdash"}, \code{5}: \code{"longdash"}, \code{6}: \code{"twodash"}, or a string of up to eight characters (between \code{"0"} and \code{"9"}) which specifies alternatively the length, in points or pixels, of the drawn elements and the blanks, for example \code{lty="44"} will have the same effect than \code{lty=2} 729 | 730 | \bcode{lwd} a numeric which controls the width of lines, default \code{1} 731 | 732 | \bcode{mar} a vector of 4 numeric values which control the space between the axes and the border of the graph of the form \code{c(bottom, left, top, right)}, the default values are \code{c(5.1, 4.1, 4.1, 2.1)} 733 | 734 | \bcode{mfcol} a vector of the form \code{c(nr,nc)} which partitions the graphic window as a matrix of \code{nr} lines and \code{nc} columns, the plots are then drawn in columns 735 | 736 | \bcode{mfrow} id. but the plots are drawn by row 737 | 738 | \bcode{pch} controls the type of symbol, either an integer between 1 739 | and 25, or any single character within \code{""} 740 | 741 | \samepage\includegraphics[width=8.5cm]{pch_symbol} 742 | 743 | \bcode{ps} size in points of texts and symbols as integer 744 | 745 | \bcode{pty} a character which specifies the type of the plotting region, \code{"s"}: square, \code{"m"}: maximal 746 | 747 | \bcode{tck} a value which specifies the length of tick-marks on the axes as a fraction of the smallest of the width or height of the plot; if \code{tck=1} a grid is drawn 748 | 749 | \bcode{tcl} a value which specifies the length of tick-marks on the axes as a fraction of the height of a line of text (by default \code{tcl=-0.5}) 750 | 751 | \bcode{xaxt} if \code{xaxt="n"} the $x$-axis is set but not drawn (useful in conjonction with \code{axis(side=1, ...)}) 752 | 753 | \bcode{yaxt} if \code{yaxt="n"} the $y$-axis is set but not drawn (useful in conjonction with \code{axis(side=2, ...)}) 754 | 755 | 756 | 757 | \section{ggplot2} 758 | \bcode{qplot(x=vx, y=vy, data=df)} plots columns df\$vx and df\$vy 759 | \bcode{ggsave()} save the last plot 760 | 761 | \section{Lattice (Trellis) graphics} 762 | \everypar={\hangindent=9mm} 763 | 764 | \bcode{xyplot(y\~{}x)} bivariate plots (with many functionalities) 765 | 766 | \bcode{barchart(y\~{}x)} histogram of the values of \code{y} with 767 | respect to those of \code{x} 768 | 769 | \bcode{dotplot(y\~{}x)} Cleveland dot plot (stacked plots line-by-line 770 | and column-by-column) 771 | 772 | \bcode{densityplot(\~{}x)} density functions plot 773 | 774 | \bcode{histogram(\~{}x)} histogram of the frequencies of \code{x} 775 | 776 | \bcode{bwplot(y\~{}x)} ``box-and-whiskers'' plot 777 | 778 | \bcode{qqmath(\~{}x)} quantiles of \code{x} with respect to the values expected under a theoretical distribution 779 | 780 | \bcode{stripplot(y\~{}x)} single dimension plot, \code{x} must be numeric, \code{y} may be a factor 781 | 782 | \bcode{qq(y\~{}x)} quantiles to compare two distributions, \code{x} must be numeric, \code{y} may be numeric, character, or factor but must have two `levels' 783 | 784 | \bcode{splom(\~{}x)} matrix of bivariate plots 785 | 786 | \bcode{parallel(\~{}x)} parallel coordinates plot 787 | 788 | \bcode{levelplot(z\~{}x*y|g1*g2)} coloured plot of the values of \code{z} at the coordinates given by \code{x} and \code{y} (\code{x}, \code{y} and \code{z} are all of the same length) 789 | 790 | \bcode{wireframe(z\~{}x*y|g1*g2)} 3d surface plot 791 | 792 | \bcode{cloud(z\~{}x*y|g1*g2)} 3d scatter plot 793 | 794 | \everypar={\hangindent=0mm} 795 | In the normal Lattice formula, \code{y~x|g1*g2} has 796 | combinations of optional conditioning variables \code{g1} 797 | and \code{g2} plotted on separate panels. Lattice functions 798 | take many of the same arguments as base 799 | graphics plus also \code{data=} the data frame for the formula 800 | variables and \code{subset=} for subsetting. Use \code{panel=} to 801 | define a custom panel function (see \code{apropos("panel")} 802 | and \code{?llines}). Lattice functions return an object of class 803 | trellis and have to be \code{print}-ed to produce the graph. Use 804 | \code{print(xyplot(...))} inside functions where automatic 805 | printing doesn't work. Use \code{lattice.theme} and \code{lset} to 806 | change Lattice defaults. 807 | 808 | 809 | 810 | \section{Optimization and model fitting} 811 | \everypar={\hangindent=9mm} 812 | 813 | \bcode{optim(par, fn, method = c("Nelder-Mead", "BFGS", "CG", 814 | "L-BFGS-B", "SANN")} general-purpose optimization; \code{par} is 815 | initial values, \code{fn} is function to optimize (normally minimize) 816 | 817 | \bcode{nlm(f,p)} minimize function \code{f} using a Newton-type 818 | algorithm with starting values \code{p} 819 | 820 | \bcode{lm(formula)} fit linear models; \code{formula} is typically of 821 | the form \code{response ~ termA + termB + ...}; use \code{I(x*y) 822 | + I(x\^{}2)} for terms made of nonlinear components 823 | 824 | \bcode{glm(formula,family=)} fit generalized linear models, specified by 825 | giving a symbolic description of the linear predictor and a 826 | description of the error distribution; \code{family} is a 827 | description of the error distribution and link function to 828 | be used in the model; see \code{?family} 829 | 830 | \bcode{nls(formula)} nonlinear least-squares estimates of the nonlinear 831 | model parameters 832 | 833 | \bcode{approx(x,y=)} linearly interpolate given data points; \code{x} can be an 834 | xy plotting structure 835 | 836 | \bcode{spline(x,y=)} cubic spline interpolation 837 | 838 | \bcode{loess(formula)} fit a polynomial surface using local fitting 839 | 840 | \everypar={\hangindent=0mm} 841 | Many of the formula-based modeling functions have several common 842 | arguments: \code{data=} the data frame for the formula variables, 843 | \code{subset=} a subset of variables used in the fit, 844 | \code{na.action=} action for missing values: \code{"na.fail"}, \code{"na.omit"}, or 845 | a function. The following generics often apply to model fitting functions: 846 | 847 | \everypar={\hangindent=9mm} 848 | \bcode{predict(fit,...)} predictions from \code{fit} based on input data 849 | 850 | \bcode{df.residual(fit)} returns the number of residual degrees of freedom 851 | 852 | \bcode{coef(fit)} returns the estimated coefficients (sometimes with their standard-errors) 853 | 854 | \bcode{residuals(fit)} returns the residuals 855 | 856 | \bcode{deviance(fit)} returns the deviance 857 | 858 | \bcode{fitted(fit)} returns the fitted values 859 | 860 | \bcode{logLik(fit)} computes the logarithm of the likelihood and the number of parameters 861 | 862 | \bcode{AIC(fit)} computes the Akaike information criterion or AIC 863 | 864 | 865 | \section{Statistics} 866 | \everypar={\hangindent=9mm} 867 | 868 | \bcode{aov(formula)} analysis of variance model 869 | 870 | \bcode{anova(fit,...)} analysis of variance (or deviance) tables for one or more 871 | fitted model objects 872 | 873 | \bcode{density(x)} kernel density estimates of \code{x} 874 | 875 | \bcode{binom.test()}, \bcode{pairwise.t.test()}, \bcode{power.t.test()}, 876 | \bcode{prop.test()}, \bcode{t.test()}, ... use 877 | \code{help.search("test")} 878 | 879 | 880 | 881 | 882 | 883 | \section{Distributions} 884 | 885 | \bcode{rnorm(n, mean=0, sd=1)} Gaussian (normal) 886 | 887 | \bcode{rexp(n, rate=1)} exponential 888 | 889 | \bcode{rgamma(n, shape, scale=1)} gamma 890 | 891 | \bcode{rpois(n, lambda)} Poisson 892 | 893 | \bcode{rweibull(n, shape, scale=1)} Weibull 894 | 895 | \bcode{rcauchy(n, location=0, scale=1)} Cauchy 896 | 897 | \bcode{rbeta(n, shape1, shape2)} beta 898 | 899 | \bcode{rt(n, df)} `Student' ($t$) 900 | 901 | \bcode{rf(n, df1, df2)} Fisher--Snedecor ($F$) ($\chi^2$) 902 | 903 | \bcode{rchisq(n, df)} Pearson 904 | 905 | \bcode{rbinom(n, size, prob)} binomial 906 | 907 | \bcode{rgeom(n, prob)} geometric 908 | 909 | \bcode{rhyper(nn, m, n, k)} hypergeometric 910 | 911 | \bcode{rlogis(n, location=0, scale=1)} logistic 912 | 913 | \bcode{rlnorm(n, meanlog=0, sdlog=1)} lognormal 914 | 915 | \bcode{rnbinom(n, size, prob)} negative binomial 916 | 917 | \bcode{runif(n, min=0, max=1)} uniform 918 | 919 | \bcode{rwilcox(nn, m, n)}, \code{rsignrank(nn, n)} Wilcoxon's statistics 920 | 921 | All these functions can be used by replacing the letter \code{r} with 922 | \code{d}, \code{p} or \code{q} to get, respectively, the probability 923 | density (\code{d\textsl{func}(x, ...)}), the cumulative probability 924 | density (\code{p\textsl{func}(x, ...)}), and the value of quantile 925 | (\code{q\textsl{func}(p, ...)}, with 0 $<$ \code{p} $<$ 1). 926 | 927 | 928 | 929 | 930 | 931 | \section{Programming} 932 | \everypar={\hangindent=9mm} 933 | 934 | \bcode{function( arglist ) expr} function definition 935 | 936 | \bcode{return(value)} 937 | 938 | \everypar={\hangindent=0mm} 939 | \bcode{if(cond) expr\\ 940 | if(cond) cons.expr else alt.expr\\ 941 | for(var in seq) expr\\ 942 | while(cond) expr\\ 943 | repeat expr\\ 944 | break\\ 945 | next} 946 | 947 | Use braces \{\} around statements 948 | 949 | 950 | \everypar={\hangindent=9mm} 951 | \bcode{ifelse(test, yes, no)} a value with the same shape as \code{test} filled with elements 952 | from either \code{yes} or \code{no} 953 | 954 | \bcode{do.call(funname, args)} executes a function call from the name of 955 | the function and a list of arguments to be passed to it 956 | 957 | \end{multicols*} 958 | \end{document} 959 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * History of this document 2 | - Jonas Stein CC-BY-SA 2012-01-11 3 | - Tom Short granted to the public domain 2004-11-07 and got permission to include material from 4 | - Emmanuel Paradis (R for Beginners) 5 | 6 | * License CC BY-SA 7 | - http://creativecommons.org/licenses/by-sa/3.0/ 8 | - http://creativecommons.org/licenses/by-sa/3.0/legalcode 9 | 10 | * Links 11 | - pitfalls in R explained http://www.burns-stat.com/pages/Tutor/R_inferno.pdf 12 | -------------------------------------------------------------------------------- /Rpad-refcard.tex: -------------------------------------------------------------------------------- 1 | % by Tom Short, tshort@epri-peac.com, EPRI PEAC 2 | % granted to the public domain 3 | \documentclass[10pt,landscape]{article} 4 | %\documentclass[10pt,portrait]{article} 5 | \usepackage{pslatex} 6 | \usepackage{graphicx} 7 | \usepackage{multicol} 8 | \usepackage{calc} 9 | \usepackage{color} 10 | 11 | % Turn off header and footer 12 | \pagestyle{empty} 13 | 14 | %\setlength{\leftmargin}{0.75in} 15 | \setlength{\oddsidemargin}{-0.75in} 16 | \setlength{\evensidemargin}{-0.75in} 17 | \setlength{\textwidth}{10.5in} 18 | 19 | 20 | \setlength{\topmargin}{-0.2in} 21 | \setlength{\textheight}{7.4in} 22 | \setlength{\headheight}{0in} 23 | \setlength{\headsep}{0in} 24 | 25 | \pdfpageheight\paperheight 26 | \pdfpagewidth\paperwidth 27 | 28 | % Redefine section commands to use less space 29 | \makeatletter 30 | \renewcommand\section{\@startsection{section}{1}{0mm}% 31 | {-24pt}% \@plus -12pt \@minus -6pt}% 32 | {0.5ex}% 33 | {\color{blue}\normalfont\large\bfseries}} 34 | \makeatother 35 | 36 | % Don't print section numbers 37 | \setcounter{secnumdepth}{0} 38 | 39 | \setlength{\parindent}{0pt} 40 | \setlength{\parskip}{0pt} 41 | 42 | \newcommand{\code}{\texttt} 43 | \newcommand{\bcode}[1]{\texttt{\textbf{#1}}} 44 | \newcommand\F{\code{FALSE}} 45 | \newcommand\T{\code{TRUE}} 46 | 47 | %\newcommand{\hangpara}[2]{\hangindent#1\hangafter#2\noindent} 48 | %\newenvironment{hangparas}[2]{\setlength{\parindent}{\z@}\everypar={\hangpara{#1}{#2}}} 49 | 50 | \newcommand{\describe}[1]{\begin{description}{#1}\end{description}} 51 | 52 | 53 | % ----------------------------------------------------------------------- 54 | 55 | \begin{document} 56 | 57 | %\raggedright 58 | \footnotesize 59 | \begin{multicols}{3} 60 | 61 | % multicol parameters 62 | % These lengths are set only within the two main columns 63 | %\setlength{\columnseprule}{0.25pt} 64 | \setlength{\premulticols}{1pt} 65 | \setlength{\postmulticols}{1pt} 66 | \setlength{\multicolsep}{1pt} 67 | \setlength{\columnsep}{2pt} 68 | 69 | \begin{center} 70 | \Large{\textbf{\color{blue}Rpad and R Reference Card}} \\ 71 | \end{center} 72 | 73 | by Tom Short, EPRI PEAC, tshort@epri-peac.com 2004-10-21\\ 74 | Granted to the public domain. See www.Rpad.org for the source and latest version. 75 | Includes material from \emph{R for Beginners} by Emmanuel Paradis (with permission). 76 | 77 | 78 | 79 | \section{Getting help} 80 | \everypar={\hangindent=9mm} 81 | 82 | Most R functions have online documentation. 83 | 84 | \bcode{help(topic)} documentation on \code{topic} 85 | 86 | \bcode{?topic} id. 87 | 88 | \bcode{help.search("topic")} search the help system 89 | 90 | \bcode{apropos("topic")} the names of all objects in the search list 91 | matching the regular expression "topic" 92 | 93 | \bcode{help.start()} start the HTML version of help 94 | 95 | \bcode{str(a)} display the internal *str*ucture of an R object 96 | 97 | \bcode{summary(a)} gives a ``summary'' of \code{a}, usually a 98 | statistical summary but it is \emph{generic} meaning it has different operations for different 99 | classes of \code{a} 100 | 101 | \bcode{ls()} show objects in the search path; specify \code{pat="pat"} 102 | to search on a pattern 103 | 104 | \bcode{ls.str()} str() for each variable in the search path 105 | 106 | \bcode{dir()} show files in the current directory 107 | 108 | \bcode{methods(a)} shows S3 methods of \code{a} 109 | 110 | \bcode{methods(class=class(a))} lists all the methods to handle objects 111 | of class a 112 | 113 | 114 | 115 | 116 | \section{Input and output} 117 | \everypar={\hangindent=9mm} 118 | 119 | \bcode{load()} load the datasets written with \code{save} 120 | 121 | \bcode{data(x)} loads specified data sets 122 | 123 | \bcode{library(x)} load add-on packages 124 | 125 | \bcode{read.table(file)} reads a file in table format and 126 | creates a data frame from it; the default separator 127 | \code{sep=""} is any whitespace; use \code{header=TRUE} 128 | to read the first line as a header of column names; use \code{as.is=TRUE} to 129 | prevent character vectors from being converted to 130 | factors; use \code{comment.char=""} to prevent 131 | \code{"\#"} from being interpreted as a comment; use 132 | \code{skip=n} to skip \code{n} lines before reading data; see 133 | the help for options on row naming, NA treatment, and 134 | others 135 | 136 | \bcode{read.csv("filename",header=TRUE)} id. but with defaults set for reading 137 | comma-delimited files 138 | 139 | \bcode{read.delim("filename",header=TRUE)} id. but with defaults set for reading 140 | tab-delimited files 141 | 142 | \bcode{read.fwf(file,widths,header=FALSE,sep="\t",as.is=FALSE)} read a table of \emph{f}ixed \emph{w}idth \emph{f}ormatted data into a 143 | 'data.frame'; \code{widths} is an integer vector, giving the widths of the fixed-width fields 144 | 145 | \bcode{save(file,...)} saves the specified objects (...) in the XDR 146 | platform-independent binary format 147 | 148 | \bcode{save.image(file)} saves all objects 149 | 150 | \bcode{cat(..., file="", sep=" ")} prints the arguments after coercing to 151 | character; \code{sep} is the character separator between arguments 152 | 153 | \bcode{print(a, ...)} prints its arguments; generic, meaning it can 154 | have different methods for different objects 155 | 156 | \bcode{format(x,...)} format an R object for pretty printing 157 | 158 | \bcode{write.table(x,file="",row.names=TRUE,col.names=TRUE, sep=" ")} 159 | prints \code{x} after converting to a data frame; if \code{quote} is 160 | \code{TRUE}, character or factor columns are 161 | surrounded by quotes (\code{"}); \code{sep} is the field 162 | separator; \code{eol} is the end-of-line separator; 163 | \code{na} is the string for missing values; use 164 | \code{col.names=NA} to add a blank column header to 165 | get the column headers aligned correctly for 166 | spreadsheet input 167 | 168 | \bcode{sink(file)} output to \code{file}, until \code{sink()} 169 | 170 | \everypar={\hangindent=0mm} 171 | Most of the I/O functions have a \code{file} argument. This can often 172 | be a character string naming a file or a connection. \code{file=""} means the standard input or 173 | output. Connections can include files, pipes, zipped 174 | files, and R variables. 175 | 176 | On windows, the file connection can also be used with \code{description = 177 | "clipboard"}. To read a table copied from Excel, use \\ 178 | \code{x <- read.delim("clipboard")}\\ 179 | To write a table to the clipboard for Excel, use \\ 180 | \code{write.table(x,"clipboard",sep="\textbackslash t",col.names=NA)} 181 | 182 | For database interaction, see packages \code{RODBC}, \code{DBI}, 183 | \code{RMySQL}, \code{RPgSQL}, and \code{ROracle}. See packages \code{XML}, \code{hdf5}, \code{netCDF} for reading 184 | other file formats. 185 | 186 | 187 | 188 | 189 | 190 | \section{Data creation} 191 | \everypar={\hangindent=9mm} 192 | 193 | \bcode{c(...)} generic function to combine arguments with the default 194 | forming a vector; 195 | with \code{recursive=TRUE} descends through lists combining all elements 196 | into one vector 197 | 198 | \bcode{from:to} generates a sequence; ``:'' has operator priority; 1:4 199 | + 1 is ``2,3,4,5'' 200 | 201 | \bcode{seq(from,to)} generates a sequence 202 | \code{by=} specifies increment; \code{length=} specifies desired length 203 | 204 | \bcode{seq(along=x)} generates \code{1, 2, ..., length(along)}; useful for 205 | \code{for} loops 206 | 207 | \bcode{rep(x,times)} replicate \code{x} \code{times}; use \code{each=} 208 | to repeat ``each'' element of \code{x} \code{each} times; 209 | \code{rep(c(1,2,3),2)} is 1 2 3 1 2 3; \code{rep(c(1,2,3),each=2)} is 1 1 2 2 3 3 210 | 211 | \bcode{data.frame(...)} create a data frame of the named or unnamed arguments; 212 | \code{data.frame(v=1:4,ch=c("a","B","c","d"),n=10)}; shorter vectors 213 | are recycled to the length of the longest 214 | 215 | \bcode{list(...)} create a list of the named or unnamed arguments; 216 | \code{list(a=c(1,2),b="hi",c=3i)}; 217 | 218 | \bcode{array(x,dim=)} array with data \code{x}; specify 219 | dimensions like \code{dim=c(3,4,2)}; elements of \code{x} recycle if \code{x} 220 | is not long enough 221 | 222 | \bcode{matrix(x,nrow=,ncol=)} matrix; elements of \code{x} recycle 223 | 224 | \bcode{factor(x,levels=)} encodes a vector \code{x} as a factor 225 | 226 | \bcode{gl(n,k,length=n*k,labels=1:n)} generate levels (factors) by specifying 227 | the pattern of their levels; \code{k} is the number of levels, and \code{n} is the 228 | number of replications 229 | 230 | \bcode{expand.grid()} a data frame from all combinations of the supplied vectors 231 | or factors 232 | 233 | \bcode{rbind(...)} combine arguments by rows for matrices, data frames, and 234 | others 235 | 236 | \bcode{cbind(...)} id. by columns 237 | 238 | 239 | 240 | 241 | 242 | \section{Slicing and extracting data} 243 | 244 | Indexing vectors 245 | 246 | \begin{tabular}{@{}l@{\ }l} 247 | \code{x[n]} & \code{n}$^{th}$ element\\ 248 | \code{x[-n]} & all \emph{but} the \code{n}$^{th}$ element\\ 249 | \code{x[1:n]} & first \code{n} elements\\ 250 | \code{x[-(1:n)]} & elements from \code{n+1} to the end\\ 251 | \code{x[c(1,4,2)]} & specific elements\\ 252 | \code{x["name"]} & element named \code{"name"}\\ 253 | \code{x[x > 3]} & all elements greater than 3\\ 254 | \code{x[x > 3 \& x < 5]} & all elements between 3 and 5\\ 255 | \code{x[x \%in\% c("a","and","the")]} & elements in the given set\\ 256 | \end{tabular} 257 | 258 | Indexing lists\\* 259 | \samepage\begin{tabular}{@{}l@{\ }l} 260 | \code{x[n]} & list with elements \code{n}\\ 261 | \code{x[[n]]} & \code{n}$^{th}$ element of the list\\ 262 | \code{x[["name"]]} & element of the list named \code{"name"}\\ 263 | \code{x\$name} & id.\\ 264 | \end{tabular} 265 | 266 | Indexing matrices 267 | 268 | \begin{tabular}{@{}l@{\ }l} 269 | \code{x[i,j]} & element at row \code{i}, column \code{j}\\ 270 | \code{x[i,]} & row \code{i}\\ 271 | \code{x[,j]} & column \code{j}\\ 272 | \code{x[,c(1,3)]} & columns 1 and 3\\ 273 | \code{x["name",]} & row named \code{"name"}\\ 274 | \end{tabular} 275 | 276 | Indexing data frames (matrix indexing plus the following) 277 | 278 | \begin{tabular}{@{}l@{\ }l} 279 | \code{x[["name"]]} & column named \code{"name"}\\ 280 | \code{x\$name} & id.\\ 281 | \end{tabular} 282 | 283 | 284 | 285 | 286 | 287 | \section{Variable conversion} 288 | \everypar={\hangindent=9mm} 289 | 290 | \bcode{as.array(x), as.data.frame(x), as.numeric(x), as.logical(x), 291 | as.complex(x), as.character(x), ...} convert type; for a complete list, 292 | use \code{methods(as)} 293 | 294 | 295 | 296 | 297 | \section{Variable information} 298 | \everypar={\hangindent=9mm} 299 | 300 | \bcode{is.na(x), is.null(x), is.array(x), is.data.frame(x), is.numeric(x), 301 | is.complex(x), is.character(x), ...} test for type; for a complete list, 302 | use \code{methods(is)} 303 | 304 | \bcode{length(x)} number of elements in \code{x} 305 | 306 | \bcode{dim(x)} Retrieve or set the dimension of an object; 307 | \code{dim(x) <- c(3,2)} 308 | 309 | \bcode{dimnames(x)} Retrieve or set the dimension names of an object 310 | 311 | \bcode{nrow(x)} number of rows; \code{NROW(x)} is the same but treats 312 | a vector as a one-row matrix 313 | 314 | \bcode{ncol(x)} and \bcode{NCOL(x)} id. for columns 315 | 316 | \bcode{class(x)} get or set the class of \code{x}; \code{class(x) <- "myclass"} 317 | 318 | \bcode{unclass(x)} remove the class attribute of \code{x} 319 | 320 | \bcode{attr(x,which)} get or set the attribute \code{which} of \code{x} 321 | 322 | \bcode{attributes(obj)} get or set the list of attributes of \code{obj} 323 | 324 | 325 | 326 | 327 | 328 | \section{Data selection and manipulation} 329 | \everypar={\hangindent=9mm} 330 | 331 | \bcode{which.max(x)} returns the index of the greatest element of 332 | \bcode{x} 333 | 334 | \bcode{which.min(x)} returns the index of the smallest element of 335 | \bcode{x} 336 | 337 | \bcode{rev(x)} reverses the elements of \code{x} 338 | 339 | \bcode{sort(x)} sorts the elements of \code{x} in increasing order; to sort in decreasing order: \code{rev(sort(x))} 340 | 341 | \bcode{cut(x,breaks)} divides \code{x} into intervals (factors); 342 | \code{breaks} is the number of cut intervals or a vector of cut points 343 | 344 | \bcode{match(x, y)} returns a vector of the same length than \code{x} with the elements of \code{x} which are in \code{y} (\code{NA} otherwise) 345 | 346 | \bcode{which(x == a)} returns a vector of the indices of \code{x} if the comparison operation is true (\T), in this example the values of \code{i} for which \code{x[i] == a} (the argument of this function must be a variable of mode logical) 347 | 348 | \bcode{choose(n, k)} computes the combinations of $k$ events among $n$ repetitions = $n!/[(n-k)!k!]$ 349 | 350 | \bcode{na.omit(x)} suppresses the observations with missing data (\code{NA}) (suppresses the corresponding line if \code{x} is a matrix or a data frame) 351 | 352 | \bcode{na.fail(x)} returns an error message if \code{x} contains at least one \code{NA} 353 | 354 | \bcode{unique(x)} if \code{x} is a vector or a data frame, returns a similar object but with the duplicate elements suppressed 355 | 356 | \bcode{table(x)} returns a table with the numbers of the differents values of \code{x} (typically for integers or factors) 357 | 358 | \bcode{subset(x, ...)} returns a selection of \code{x} with respect to criteria (\code{...}, typically comparisons: \code{x\$V1 < 10}); if \code{x} is a data frame, the option \code{select} gives the variables to be kept or dropped using a minus sign 359 | 360 | \bcode{sample(x, size)} resample randomly and without replacement \code{size} elements in the vector \code{x}, the option \code{replace = TRUE} allows to resample with replacement 361 | 362 | \bcode{prop.table(x,margin=)} table entries as fraction of marginal table 363 | 364 | 365 | 366 | 367 | \section{Math} 368 | \everypar={\hangindent=9mm} 369 | 370 | \bcode{sin,cos,tan,asin,acos,atan,atan2,log,log10,exp} 371 | 372 | \bcode{max(x)} maximum of the elements of \code{x} 373 | 374 | \bcode{min(x)} minimum of the elements of \code{x} 375 | 376 | \bcode{range(x)} id. then \code{c(min(x), max(x))} 377 | 378 | \bcode{sum(x)} sum of the elements of \code{x} 379 | 380 | \bcode{diff(x)} lagged and iterated differences of vector \code{x} 381 | 382 | \bcode{prod(x)} product of the elements of \code{x} 383 | 384 | \bcode{mean(x)} mean of the elements of \code{x} 385 | 386 | \bcode{median(x)} median of the elements of \code{x} 387 | 388 | \bcode{quantile(x,probs=)} sample quantiles 389 | corresponding to the given probabilities (defaults to 0,.25,.5,.75,1) 390 | 391 | \bcode{weighted.mean(x, w)} mean of \code{x} with weights \code{w} 392 | 393 | \bcode{rank(x)} ranks of the elements of \code{x} 394 | 395 | \bcode{var(x)} or \code{cov(x)} variance of the elements of \code{x} 396 | (calculated on $n-1$); if \code{x} is a matrix or a data frame, the 397 | variance-covariance matrix is calculated 398 | 399 | \bcode{sd(x)} standard deviation of \code{x} 400 | 401 | \bcode{cor(x)} correlation matrix of \code{x} if it is a matrix or a 402 | data frame (1 if \code{x} is a vector) 403 | 404 | \bcode{var(x, y)} or \code{cov(x, y)} covariance between \code{x} and \code{y}, or between the columns of \code{x} and those of \code{y} if they are matrices or data frames 405 | 406 | \bcode{cor(x, y)} linear correlation between \code{x} and \code{y}, or correlation matrix if they are matrices or data frames 407 | 408 | \bcode{round(x, n)} rounds the elements of \code{x} to \code{n} 409 | decimals 410 | 411 | \bcode{log(x, base)} computes the logarithm of \code{x} with base \code{base} 412 | 413 | \bcode{scale(x)} if \code{x} is a matrix, centers and reduces the data; to center only use the option \code{center=FALSE}, to reduce only \code{scale=FALSE} (by default \code{center=TRUE, scale=TRUE}) 414 | 415 | \bcode{pmin(x,y,...)} a vector which $i$th element is the minimum of \code{x[i]}, \code{y[i]}, \ldots 416 | 417 | \bcode{pmax(x,y,...)} id. for the maximum 418 | 419 | \bcode{cumsum(x)} a vector which $i$th element is the sum from \code{x[1]} to \code{x[i]} 420 | 421 | \bcode{cumprod(x)} id. for the product 422 | 423 | \bcode{cummin(x)} id. for the minimum 424 | 425 | \bcode{cummax(x)} id. for the maximum 426 | 427 | \bcode{union(x,y)},~\bcode{intersect(x,y)},~\bcode{setdiff(x,y)},~\bcode{setequal(x,y)}, \bcode{is.element(el,set)} ``set'' functions 428 | 429 | \bcode{Re(x)} real part of a complex number 430 | 431 | \bcode{Im(x)} imaginary part 432 | 433 | \bcode{Mod(x)} modulus; \code{abs(x)} is the same 434 | 435 | \bcode{Arg(x)} angle in radians of the complex number 436 | 437 | \bcode{Conj(x)} complex conjugate 438 | 439 | \bcode{convolve(x,y)} compute the several kinds of 440 | convolutions of two sequences 441 | 442 | \bcode{fft(x)} Fast Fourier Transform of an array 443 | 444 | \bcode{mvfft(x)} FFT of each column of a matrix 445 | 446 | \bcode{filter(x,filter)} applies linear filtering to a univariate time series or to each 447 | series separately of a multivariate time series 448 | 449 | \everypar={\hangindent=0mm} 450 | Many math functions have a logical parameter \code{na.rm=FALSE} to 451 | specify missing data (NA) removal. 452 | 453 | 454 | 455 | 456 | \section{Matrices} 457 | \everypar={\hangindent=9mm} 458 | 459 | \bcode{t(x)} transpose 460 | 461 | \bcode{diag(x)} diagonal 462 | 463 | \bcode{\%*\%} matrix multiplication 464 | 465 | \bcode{solve(a,b)} solves \code{a \%*\% x = b} for \code{x} 466 | 467 | \bcode{solve(a)} matrix inverse of \code{a} 468 | 469 | \bcode{rowsum(x)} sum of rows for a matrix-like object; 470 | \bcode{rowSums(x)} is a faster version 471 | 472 | \bcode{colsum(x)}, \bcode{colSums(x)} id. for columns 473 | 474 | \bcode{rowMeans(x)} fast version of row means 475 | 476 | \bcode{colMeans(x)} id. for columns 477 | 478 | 479 | 480 | 481 | 482 | \section{Advanced data processing} 483 | \everypar={\hangindent=9mm} 484 | 485 | \bcode{apply(X,INDEX,FUN=)} a vector or array or list of values obtained by applying a 486 | function \code{FUN} to margins (\code{INDEX}) of \code{X} 487 | 488 | \bcode{lapply(X,FUN)} apply \code{FUN} to each element of the list \code{X} 489 | 490 | \bcode{tapply(X,INDEX,FUN=)} apply \code{FUN} to each cell 491 | of a ragged array given by \code{X} with indexes \code{INDEX} 492 | 493 | \bcode{by(data,INDEX,FUN)} apply \code{FUN} to data frame \code{data} 494 | subsetted by \code{INDEX} 495 | 496 | \bcode{merge(a,b)} merge two data frames by common columns or row names 497 | 498 | \bcode{xtabs(a~b,data=x)} a contingency table from cross-classifying factors 499 | 500 | \bcode{aggregate(x,by,FUN)} splits the data frame \code{x} into subsets, computes summary statistics for 501 | each, and returns the result in a convenient form; \code{by} is a 502 | list of grouping elements, each as long as the variables in \code{x} 503 | 504 | \bcode{stack(x, ...)} transform data available as 505 | separate columns in a data frame or list into a single column 506 | 507 | \bcode{unstack(x, ...)} inverse of \code{stack()} 508 | 509 | \bcode{reshape(x, ...)} reshapes a data frame between 'wide' format with 510 | repeated measurements in separate columns of the same record and 511 | 'long' format with the repeated measurements in separate records; 512 | use \code(direction="wide") or \code(direction="long") 513 | 514 | 515 | 516 | 517 | 518 | \section{Strings} 519 | \everypar={\hangindent=9mm} 520 | 521 | \bcode{paste(...)} concatenate vectors after converting to character; 522 | \code{sep=} is the string to separate terms (a single space is the default); 523 | \code{collapse=} is an optional string to separate ``collapsed'' results 524 | 525 | \bcode{substr(x,start,stop)} substrings in a character vector; can also assign, as \code{substr(x, start, stop) <- value} 526 | 527 | \bcode{strsplit(x,split)} split \code{x} according to the substring \code{split} 528 | 529 | \bcode{grep(pattern,x)} searches for matches to \code{pattern} 530 | within \code{x}; see \code{?regex} 531 | 532 | \bcode{gsub(pattern,replacement,x)} replacement of matches determined by 533 | regular expression matching \code{sub()} is the same but only 534 | replaces the first occurrence. 535 | 536 | \bcode{tolower(x)} convert to lowercase 537 | 538 | \bcode{toupper(x)} convert to uppercase 539 | 540 | \bcode{match(x,table)} a vector of the positions of first matches for the elements of \code{x} among \code{table} 541 | 542 | \bcode{x \%in\% table} id. but returns a logical vector 543 | 544 | \bcode{pmatch(x,table)} partial matches for the elements of \code{x} among \code{table} 545 | 546 | \bcode{nchar(x)} number of characters 547 | 548 | 549 | \section{\color{blue}Dates and Times} 550 | 551 | The class \code{Date} has dates without times. \code{POSIXct} has 552 | dates and times, including time zones. Comparisons (e.g. $>$), 553 | \code{seq()}, and \code{difftime()} are useful. \code{Date} also allows 554 | $+$ and $-$. \code{?DateTimeClasses} gives more information. See also package 555 | \code{chron}. 556 | 557 | \everypar={\hangindent=9mm} 558 | 559 | \bcode{as.Date(s)} and \bcode{as.POSIXct(s)} convert to the respective 560 | class; \code{format(dt)} converts to a string representation. The 561 | default string format is ``2001-02-21''. These accept a second argument 562 | to specify a format for conversion. Some common formats are: 563 | 564 | \describe{ 565 | \itemsep=0pt\parskip=0pt 566 | \item{\code{\%a}, \code{\%A}} {Abbreviated and full weekday name.} 567 | \item{\code{\%b}, \code{\%B}} {Abbreviated and full month name.} 568 | \item{\code{\%d}} {Day of the month (01--31).} 569 | \item{\code{\%H}} {Hours (00--23).} 570 | \item{\code{\%I}} {Hours (01--12).} 571 | \item{\code{\%j}} {Day of year (001--366).} 572 | \item{\code{\%m}} {Month (01--12).} 573 | \item{\code{\%M}} {Minute (00--59).} 574 | \item{\code{\%p}} {AM/PM indicator. } 575 | \item{\code{\%S}} {Second as decimal number (00--61).} 576 | \item{\code{\%U}} {Week (00--53); the first Sunday as day 1 of week 1.} 577 | \item{\code{\%w}} {Weekday (0--6, Sunday is 0).} 578 | \item{\code{\%W}} {Week (00--53); the first Monday as day 1 of week 1.} 579 | \item{\code{\%y}} {Year without century (00--99). Don't use.} 580 | \item{\code{\%Y}} {Year with century.} 581 | \item{\code{\%z}} {(output only.) Offset from Greenwich; \code{-0800} is 8 hours west of.} 582 | \item{\code{\%Z}} {(output only.) Time zone as a character 583 | string (empty if not available).} 584 | } 585 | 586 | Where leading zeros are shown they will be used on output but are 587 | optional on input. See \code{?strftime}. 588 | 589 | 590 | 591 | 592 | 593 | \section{Plotting} 594 | 595 | \everypar={\hangindent=9mm} 596 | 597 | \bcode{plot(x)} plot of the values of \code{x} (on the $y$-axis) ordered on the $x$-axis 598 | 599 | \bcode{plot(x, y)} bivariate plot of \code{x} (on the $x$-axis) and \code{y} (on the $y$-axis) 600 | 601 | \bcode{hist(x)} histogram of the frequencies of \code{x} 602 | 603 | \bcode{barplot(x)} histogram of the values of \code{x}; use 604 | \code{horiz=FALSE} for horizontal bars 605 | 606 | \bcode{dotchart(x)} if \code{x} is a data frame, plots a Cleveland dot plot (stacked plots line-by-line and column-by-column) 607 | 608 | \bcode{pie(x)} circular pie-chart 609 | 610 | \bcode{boxplot(x)} ``box-and-whiskers'' plot 611 | 612 | \bcode{sunflowerplot(x, y)} id. than \code{plot()} but the points with similar coordinates are drawn as flowers which petal number represents the number of points 613 | 614 | \bcode{stripplot(x)} plot of the values of \code{x} on a line (an alternative to \code{boxplot()} for small sample sizes) 615 | 616 | \bcode{coplot(x\~{}y $\mid$ z)} bivariate plot of \code{x} and \code{y} for each value or interval of values of \code{z} 617 | 618 | \bcode{interaction.plot (f1, f2, y)} if \code{f1} and \code{f2} are factors, plots the means of \code{y} (on the $y$-axis) with respect to the values of \code{f1} (on the $x$-axis) and of \code{f2} (different curves); the option \code{fun} allows to choose the summary statistic of \code{y} (by default \code{fun=mean}) 619 | 620 | \bcode{matplot(x,y)} bivariate plot of the first column of \code{x} {\it vs.} the first one of \code{y}, the second one of \code{x} {\it vs.} the second one of \code{y}, etc. 621 | 622 | \bcode{fourfoldplot(x)} visualizes, with quarters of circles, the association between two dichotomous variables for different populations (\code{x} must be an array with \code{dim=c(2, 2, k)}, or a matrix with \code{dim=c(2, 2)} if $k=1$) 623 | 624 | \bcode{assocplot(x)} Cohen--Friendly graph showing the deviations from independence of rows and columns in a two dimensional contingency table 625 | 626 | \bcode{mosaicplot(x)} `mosaic' graph of the residuals from a log-linear regression of a contingency table 627 | 628 | \bcode{pairs(x)} if \code{x} is a matrix or a data frame, draws all possible bivariate plots between the columns of \code{x} 629 | 630 | \bcode{plot.ts(x)} if \code{x} is an object of class \code{"ts"}, plot of \code{x} with respect to time, \code{x} may be multivariate but the series must have the same frequency and dates 631 | 632 | \bcode{ts.plot(x)} id. but if \code{x} is multivariate the series may have different dates and must have the same frequency 633 | 634 | \bcode{qqnorm(x)} quantiles of \code{x} with respect to the values expected under a normal law 635 | 636 | \bcode{qqplot(x, y)} quantiles of \code{y} with respect to the quantiles of \code{x} 637 | 638 | \bcode{contour(x, y, z)} contour plot (data are interpolated to draw the curves), \code{x} and \code{y} must be vectors and \code{z} must be a matrix so that \code{dim(z)=c(length(x), length(y))} (\code{x} and \code{y} may be omitted) 639 | 640 | \bcode{filled.contour(x, y, z)} id. but the areas between the contours are coloured, and a legend of the colours is drawn as well 641 | 642 | \bcode{image(x, y, z)} id. but with colours (actual data are plotted) 643 | 644 | \bcode{persp(x, y, z)} id. but in perspective (actual data are plotted) 645 | 646 | \bcode{stars(x)} if \code{x} is a matrix or a data frame, draws a graph with segments or a star where each row of \code{x} is represented by a star and the columns are the lengths of the segments 647 | 648 | \bcode{symbols(x, y, ...)} draws, at the coordinates given by \code{x} and \code{y}, symbols (circles, squares, rectangles, stars, thermometres or ``boxplots'') which sizes, colours \ldots{} are specified by supplementary arguments 649 | 650 | \bcode{termplot(mod.obj)} plot of the (partial) effects of a regression model (\code{mod.obj}) 651 | 652 | The following parameters are common to many plotting functions: 653 | 654 | \bcode{add=FALSE} if \T{} superposes the plot on the previous one (if it exists) 655 | 656 | \bcode{axes=TRUE} if \F{} does not draw the axes and the box 657 | 658 | \bcode{type="p"} specifies the type of plot, \code{"p"}: points, \code{"l"}: lines, \code{"b"}: points connected by lines, \code{"o"}: id. but the lines are over the points, \code{"h"}: vertical lines, \code{"s"}: steps, the data are represented by the top of the vertical lines, \code{"S"}: id. but the data are represented by the bottom of the vertical lines 659 | 660 | \bcode{xlim=, ylim=} specifies the lower and upper limits of the axes, for example with \code{xlim=c(1, 10)} or \code{xlim=range(x)} 661 | 662 | \bcode{xlab=, ylab=} annotates the axes, must be variables of mode character 663 | 664 | \bcode{main=} main title, must be a variable of mode character 665 | 666 | \bcode{sub=} sub-title (written in a smaller font) 667 | 668 | 669 | 670 | 671 | \section{Low-level plotting commands} 672 | \everypar={\hangindent=9mm} 673 | 674 | \bcode{points(x, y)} adds points (the option \code{type=} can be used) 675 | 676 | \bcode{lines(x, y)} id. but with lines 677 | 678 | \bcode{text(x, y, \mbox{labels}, ...)} adds text given by \code{labels} at coordinates (\code{x},\code{y}); a typical use is: \code{plot(x, y, type="n"); text(x, y, names)} 679 | 680 | \bcode{mtext(text, side=3, line=0, ...)} adds text given by \code{text} in the margin specified by \code{side} (see \code{axis()} below); \code{line} specifies the line from the plotting area 681 | 682 | \bcode{segments(x0, y0, x1, y1)} draws lines from points (\code{x0},\code{y0}) to points (\code{x1},\code{y1}) 683 | 684 | \bcode{arrows(x0, y0, x1, y1, angle= 30, code=2)} id. with arrows at points (\code{x0},\code{y0}) if \code{code=2}, at points (\code{x1},\code{y1}) if \code{code=1}, or both if \code{code=3}; \code{angle} controls the angle from the shaft of the arrow to the edge of the arrow head 685 | 686 | \bcode{abline(a,b)} draws a line of slope \code{b} and intercept \code{a} 687 | 688 | \bcode{abline(h=y)} draws a horizontal line at ordinate \code{y} 689 | 690 | \bcode{abline(v=x)} draws a vertical line at abcissa \code{x} 691 | 692 | \bcode{abline(lm.obj)} draws the regression line given by \code{lm.obj} 693 | 694 | \bcode{rect(x1, y1, x2, y2)} draws a rectangle which left, right, bottom, and top limits are \code{x1}, \code{x2}, \code{y1}, and \code{y2}, respectively 695 | 696 | \bcode{polygon(x, y)} draws a polygon linking the points with coordinates given by \code{x} and \code{y} 697 | 698 | \bcode{legend(x, y, legend)} adds the legend at the point (\code{x},\code{y}) with the symbols given by \code{legend} 699 | 700 | \bcode{title()} adds a title and optionally a sub-title 701 | 702 | \bcode{axis(side, vect)} adds an axis at the bottom (\code{side=1}), on the left (\code{2}), at the top (\code{3}), or on the right (\code{4}); \code{vect} (optional) gives the abcissa (or ordinates) where tick-marks are drawn 703 | 704 | \bcode{rug(x)} draws the data \code{x} on the $x$-axis as small vertical lines 705 | 706 | \bcode{locator(n, type="n", ...)} returns the coordinates ($x,y$) after the user has clicked \code{n} times on the plot with the mouse; also draws symbols (\code{type="p"}) or lines (\code{type="l"}) with respect to optional graphic parameters (\code{...}); by default nothing is drawn (\code{type="n"}) 707 | 708 | 709 | 710 | 711 | \section{Graphical parameters} 712 | 713 | These can be set globally with \bcode{par(...)}; many can be passed as 714 | parameters to plotting commands. 715 | 716 | \everypar={\hangindent=9mm} 717 | 718 | \bcode{adj} controls text justification (\code{0} left-justified, \code{0.5} centred, \code{1} right-justified) 719 | 720 | \bcode{bg} specifies the colour of the background (ex. : \code{bg="red"}, \code{bg="blue"}, \ldots{} the list of the 657 available colours is displayed with \code{colors()}) 721 | 722 | \bcode{bty} controls the type of box drawn around the plot, allowed values are: \code{"o"}, \code{"l"}, \code{"7"}, \code{"c"}, \code{"u"} ou \code{"]"} (the box looks like the corresponding character); if \code{bty="n"} the box is not drawn 723 | 724 | \bcode{cex} a value controlling the size of texts and symbols with respect to the default; the following parameters have the same control for numbers on the axes, \code{cex.axis}, the axis labels, \code{cex.lab}, the title, \code{cex.main}, and the sub-title, \code{cex.sub} 725 | 726 | \bcode{col} controls the color of symbols and lines; use color names: 727 | \code{"red"}, \code{"blue"} see \code{colors()} or as \code{"\#RRGGBB"}; 728 | see \code{rgb()}, \code{hsv()}, \code{gray()}, and \code{rainbow()}; as for \code{cex} there are: \code{col.axis}, \code{col.lab}, \code{col.main}, \code{col.sub} 729 | 730 | \bcode{font} an integer which controls the style of text (\code{1}: normal, \code{2}: italics, \code{3}: bold, \code{4}: bold italics); as for \code{cex} there are: \code{font.axis}, \code{font.lab}, \code{font.main}, \code{font.sub} 731 | 732 | \bcode{las} an integer which controls the orientation of the axis labels (\code{0}: parallel to the axes, \code{1}: horizontal, \code{2}: perpendicular to the axes, \code{3}: vertical) 733 | 734 | \bcode{lty} controls the type of lines, can be an integer or string (\code{1}: \code{"solid"}, \code{2}: \code{"dashed"}, \code{3}: \code{"dotted"}, \code{4}: \code{"dotdash"}, \code{5}: \code{"longdash"}, \code{6}: \code{"twodash"}, or a string of up to eight characters (between \code{"0"} and \code{"9"}) which specifies alternatively the length, in points or pixels, of the drawn elements and the blanks, for example \code{lty="44"} will have the same effect than \code{lty=2} 735 | 736 | \bcode{lwd} a numeric which controls the width of lines, default \code{1} 737 | 738 | \bcode{mar} a vector of 4 numeric values which control the space between the axes and the border of the graph of the form \code{c(bottom, left, top, right)}, the default values are \code{c(5.1, 4.1, 4.1, 2.1)} 739 | 740 | \bcode{mfcol} a vector of the form \code{c(nr,nc)} which partitions the graphic window as a matrix of \code{nr} lines and \code{nc} columns, the plots are then drawn in columns 741 | 742 | \bcode{mfrow} id. but the plots are drawn by row 743 | 744 | \bcode{pch} controls the type of symbol, either an integer between 1 745 | and 25, or any single character within \code{""} 746 | 747 | \samepage\includegraphics[width=8.5cm]{pch_symbol} 748 | 749 | \bcode{ps} an integer which controls the size in points of texts and symbols 750 | 751 | \bcode{pty} a character which specifies the type of the plotting region, \code{"s"}: square, \code{"m"}: maximal 752 | 753 | \bcode{tck} a value which specifies the length of tick-marks on the axes as a fraction of the smallest of the width or height of the plot; if \code{tck=1} a grid is drawn 754 | 755 | \bcode{tcl} a value which specifies the length of tick-marks on the axes as a fraction of the height of a line of text (by default \code{tcl=-0.5}) 756 | 757 | \bcode{xaxt} if \code{xaxt="n"} the $x$-axis is set but not drawn (useful in conjonction with \code{axis(side=1, ...)}) 758 | 759 | \bcode{yaxt} if \code{yaxt="n"} the $y$-axis is set but not drawn (useful in conjonction with \code{axis(side=2, ...)}) 760 | 761 | 762 | 763 | 764 | 765 | \section{Lattice (Trellis) graphics} 766 | \everypar={\hangindent=9mm} 767 | 768 | \bcode{xyplot(y\~{}x)} bivariate plots (with many functionalities) 769 | 770 | \bcode{barchart(y\~{}x)} histogram of the values of \code{y} with 771 | respect to those of \code{x} 772 | 773 | \bcode{dotplot(y\~{}x)} Cleveland dot plot (stacked plots line-by-line 774 | and column-by-column) 775 | 776 | \bcode{densityplot(\~{}x)} density functions plot 777 | 778 | \bcode{histogram(\~{}x)} histogram of the frequencies of \code{x} 779 | 780 | \bcode{bwplot(y\~{}x)} ``box-and-whiskers'' plot 781 | 782 | \bcode{qqmath(\~{}x)} quantiles of \code{x} with respect to the values expected under a theoretical distribution 783 | 784 | \bcode{stripplot(y\~{}x)} single dimension plot, \code{x} must be numeric, \code{y} may be a factor 785 | 786 | \bcode{qq(y\~{}x)} quantiles to compare two distributions, \code{x} must be numeric, \code{y} may be numeric, character, or factor but must have two `levels' 787 | 788 | \bcode{splom(\~{}x)} matrix of bivariate plots 789 | 790 | \bcode{parallel(\~{}x)} parallel coordinates plot 791 | 792 | \bcode{levelplot(z\~{}x*y|g1*g2)} coloured plot of the values of \code{z} at the coordinates given by \code{x} and \code{y} (\code{x}, \code{y} and \code{z} are all of the same length) 793 | 794 | \bcode{wireframe(z\~{}x*y|g1*g2)} 3d surface plot 795 | 796 | \bcode{cloud(z\~{}x*y|g1*g2)} 3d scatter plot 797 | 798 | \everypar={\hangindent=0mm} 799 | In the normal Lattice formula, \code{y~x|g1*g2} has 800 | combinations of optional conditioning variables \code{g1} 801 | and \code{g2} plotted on separate panels. Lattice functions 802 | take many of the same arguments as base 803 | graphics plus also \code{data=} the data frame for the formula 804 | variables and \code{subset=} for subsetting. Use \code{panel=} to 805 | define a custom panel function (see \code{apropos("panel")} 806 | and \code{?llines}). Lattice functions return an object of class 807 | trellis and have to be \code{print}-ed to produce the graph. Use 808 | \code{print(xyplot(...))} inside functions where automatic 809 | printing doesn't work. Use \code{lattice.theme} and \code{lset} to 810 | change Lattice defaults. 811 | 812 | 813 | \section{Optimization and model fitting} 814 | \everypar={\hangindent=9mm} 815 | 816 | \bcode{optim(par, fn, method = c("Nelder-Mead", "BFGS", "CG", 817 | "L-BFGS-B", "SANN")} general-purpose optimization; \code{par} is 818 | initial values, \code{fn} is function to optimize (normally minimize) 819 | 820 | \bcode{nlm(f,p)} minimize function \code{f} using a Newton-type 821 | algorithm with starting values \code{p} 822 | 823 | \bcode{lm(formula)} fit linear models; \code{formula} is typically of 824 | the form \code{response ~ termA + termB + ...}; use \code{I(x*y) 825 | + I(x\^{}2)} for terms made of nonlinear components 826 | 827 | \bcode{glm(formula,family=)} fit generalized linear models, specified by 828 | giving a symbolic description of the linear predictor and a 829 | description of the error distribution; \code{family} is a 830 | description of the error distribution and link function to 831 | be used in the model; see \code{?family} 832 | 833 | \bcode{nls(formula)} nonlinear least-squares estimates of the nonlinear 834 | model parameters 835 | 836 | \bcode{approx(x,y=)} linearly interpolate given data points; \code{x} can be an 837 | xy plotting structure 838 | 839 | \bcode{spline(x,y=)} cubic spline interpolation 840 | 841 | \bcode{loess(formula)} fit a polynomial surface using local fitting 842 | 843 | \everypar={\hangindent=0mm} 844 | Many of the formula-based modeling functions have several common 845 | arguments: \code{data=} the data frame for the formula variables, 846 | \code{subset=} a subset of variables used in the fit, 847 | \code{na.action=} action for missing values: \code{"na.fail"}, \code{"na.omit"}, or 848 | a function. The following generics often apply to model fitting functions: 849 | 850 | \everypar={\hangindent=9mm} 851 | \bcode{predict(fit,...)} predictions from \code{fit} based on input data 852 | 853 | \bcode{df.residual(fit)} returns the number of residual degrees of freedom 854 | 855 | \bcode{coef(fit)} returns the estimated coefficients (sometimes with their standard-errors) 856 | 857 | \bcode{residuals(fit)} returns the residuals 858 | 859 | \bcode{deviance(fit)} returns the deviance 860 | 861 | \bcode{fitted(fit)} returns the fitted values 862 | 863 | \bcode{logLik(fit)} computes the logarithm of the likelihood and the number of parameters 864 | 865 | \bcode{AIC(fit)} computes the Akaike information criterion or AIC 866 | 867 | 868 | \section{Statistics} 869 | \everypar={\hangindent=9mm} 870 | 871 | \bcode{aov(formula)} analysis of variance model 872 | 873 | \bcode{anova(fit,...)} analysis of variance (or deviance) tables for one or more 874 | fitted model objects 875 | 876 | \bcode{density(x)} kernel density estimates of \code{x} 877 | 878 | \bcode{binom.test()}, \bcode{pairwise.t.test()}, \bcode{power.t.test()}, 879 | \bcode{prop.test()}, \bcode{t.test()}, ... use 880 | \code{help.search("test")} 881 | 882 | 883 | 884 | 885 | 886 | \section{Distributions} 887 | 888 | \bcode{rnorm(n, mean=0, sd=1)} Gaussian (normal) 889 | 890 | \bcode{rexp(n, rate=1)} exponential 891 | 892 | \bcode{rgamma(n, shape, scale=1)} gamma 893 | 894 | \bcode{rpois(n, lambda)} Poisson 895 | 896 | \bcode{rweibull(n, shape, scale=1)} Weibull 897 | 898 | \bcode{rcauchy(n, location=0, scale=1)} Cauchy 899 | 900 | \bcode{rbeta(n, shape1, shape2)} beta 901 | 902 | \bcode{rt(n, df)} `Student' ($t$) 903 | 904 | \bcode{rf(n, df1, df2)} Fisher--Snedecor ($F$) ($\chi^2$) 905 | 906 | \bcode{rchisq(n, df)} Pearson 907 | 908 | \bcode{rbinom(n, size, prob)} binomial 909 | 910 | \bcode{rgeom(n, prob)} geometric 911 | 912 | \bcode{rhyper(nn, m, n, k)} hypergeometric 913 | 914 | \bcode{rlogis(n, location=0, scale=1)} logistic 915 | 916 | \bcode{rlnorm(n, meanlog=0, sdlog=1)} lognormal 917 | 918 | \bcode{rnbinom(n, size, prob)} negative binomial 919 | 920 | \bcode{runif(n, min=0, max=1)} uniform 921 | 922 | \bcode{rwilcox(nn, m, n)}, \code{rsignrank(nn, n)} Wilcoxon's statistics 923 | 924 | All these functions can be used by replacing the letter \code{r} with 925 | \code{d}, \code{p} or \code{q} to get, respectively, the probability 926 | density (\code{d\textsl{func}(x, ...)}), the cumulative probability 927 | density (\code{p\textsl{func}(x, ...)}), and the value of quantile 928 | (\code{q\textsl{func}(p, ...)}, with 0 $<$ \code{p} $<$ 1). 929 | 930 | 931 | 932 | 933 | 934 | \section{Programming} 935 | \everypar={\hangindent=9mm} 936 | 937 | \bcode{function( arglist ) expr} function definition 938 | 939 | \bcode{return(value)} 940 | 941 | \everypar={\hangindent=0mm} 942 | \bcode{if(cond) expr\\ 943 | if(cond) cons.expr else alt.expr\\ 944 | for(var in seq) expr\\ 945 | while(cond) expr\\ 946 | repeat expr\\ 947 | break\\ 948 | next} 949 | 950 | Use braces \{\} around statements 951 | 952 | 953 | \everypar={\hangindent=9mm} 954 | \bcode{ifelse(test, yes, no)} a value with the same shape as \code{test} filled with elements 955 | from either \code{yes} or \code{no} 956 | 957 | \bcode{do.call(funname, args)} executes a function call from the name of 958 | the function and a list of arguments to be passed to it 959 | 960 | 961 | 962 | \section{\color{blue}Rpad HTML utilities} 963 | \everypar={\hangindent=9mm} 964 | 965 | \bcode{HTML(x)} outputs an HTML representation of an object (uses 966 | package \code{R2HTML}) 967 | 968 | \bcode{RpadDir} current Rpad directory 969 | 970 | \bcode{HTMLon()} turn on HTML mode (the default is text) 971 | 972 | \bcode{HTMLoff()} turn off HTML mode 973 | 974 | \bcode{ROutputMode()} changes how R behaves with automatic 975 | printing; possible values are: \code{"text"} (the default), \code{"html"}, or \code{"none"}. \code{"text"} is like 976 | the command line: values returned in the script are automatically printed (without an explicit \code{print} 977 | statement) in standard text format. With \code{"html"}, values returned are automatically printed, but HTML output is generated by 978 | using the \code{HTML} method instead of the \code{print} method. \code{ROutputMode} applies to all subsequent Rpad input 979 | section, including a rollover back to the beginning when a page is run 980 | several times. 981 | 982 | \bcode{HTMLh1(text), HTMLh2(text), ...} output HTML headers H1, H2, ... 983 | 984 | \bcode{HTMLargs(...)} returns a string with the arguments as \code{"a='arg1'} 985 | \code{b='arg2'"}, and so on 986 | 987 | \bcode{HTMLelement(tagName, ...)} output HTML tag \code{"tagName"} with arguments given by ... 988 | 989 | \bcode{HTMLradio(variableName, commonName="radio", text="")} 990 | output HTML for a radio input with an R variable name 991 | \code{variableName}; \code{commonName} links it with other radio 992 | elements; \code{text} specifies an adjacent label 993 | 994 | \bcode{HTMLcheckbox(name, text="", checked=FALSE)} 995 | output HTML for a checkbox input with an R variable name \code{name} 996 | 997 | \bcode{HTMLselect(name, text, default=1, size=1)} 998 | output HTML for a select box with an R variable name \code{name} 999 | with the options \code{text} 1000 | 1001 | \bcode{HTMLinput(name, value="", rpadtype="Rvariable")} 1002 | output HTML for an input box with an R variable name \code{name} and 1003 | default \code{value}; 1004 | \code{rpadtype} can by \code{"Rstring"} or \code{"Rvariable"} 1005 | 1006 | \bcode{HTMLlink(url, text)} 1007 | output HTML for a link wrapped around \code{text} 1008 | 1009 | \bcode{HTMLimg(filename)} 1010 | output HTML for an img 1011 | 1012 | \bcode{HTMLembed(filename,width=600,height=600)} 1013 | output HTML for an embed (useful for pdf or svg 1014 | 1015 | 1016 | 1017 | 1018 | \section{Rpad plotting utilities} 1019 | \everypar={\hangindent=9mm} 1020 | 1021 | \bcode{newgraph(name="",...)} sets up the graphics device (not needed unless you 1022 | want to change parameters) 1023 | 1024 | \bcode{showgraph()} generates the HTML to show the graph and runs 1025 | \code{newgraph} to advance 1026 | to the next graphics file; \code{link=TRUE} creates a link to the EPS file 1027 | 1028 | \bcode{graphoptions(...)} changes the defaults for subsequent graphs 1029 | 1030 | \everypar={\hangindent=0mm} 1031 | \code{newgraph} and \code{graphoptions} have the following options 1032 | with the defaults given: \code{type="pngalpha", res=120, 1033 | width=3.5, height=, pointsize=10, sublines=0, 1034 | toplines=.6, ratio=4/3, leftlines=0, lwd=0.6} 1035 | 1036 | \end{multicols} 1037 | \end{document} 1038 | -------------------------------------------------------------------------------- /makePchPdf.R: -------------------------------------------------------------------------------- 1 | makePchPdf <- function() { 2 | require(grid) 3 | # pdf('pch_symbol.pdf', width=2, height=1, pointsize = 10) 4 | pdf('pch_symbol.pdf', width=6, height=3, pointsize = 10) 5 | 6 | Pex=1 7 | # stuff taken from ?points 8 | ipch <- 1:(np <- 25+5) 9 | k <- 15 10 | iy <- rev((ipch-1) %/% k + .5) / k 11 | ix <- rev((k-1)-(ipch-1) %% k + .5) / k 12 | pch <- as.list(ipch) 13 | pch[25+ 1:5] <- as.list(c("*",".", "X","a","?")) 14 | print(ix) 15 | print(iy) 16 | for(i in 1:np) { 17 | pc <- pch[[i]] 18 | grid.points(unit(ix[i] + .025,"npc"), unit(iy[i],"npc"), pch = pc, size = unit(Pex/50,"npc"), gp = gpar(col = "black", fill = "lightgray", lwd=1), default.units="npc") 19 | ## red symbols with a yellow interior (where available) 20 | grid.text(pc, ix[i], iy[i]) 21 | } 22 | dev.off() 23 | } 24 | -------------------------------------------------------------------------------- /pch_symbol.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonasstein/R-Reference-Card/86f864845183f1228701e23167e421f2cd16ef2d/pch_symbol.pdf -------------------------------------------------------------------------------- /upload_pdf.lftp: -------------------------------------------------------------------------------- 1 | open ftp://cran.r-project.org/ 2 | cd incoming/ 3 | put R-refcard.pdf --------------------------------------------------------------------------------