R Reference Card
Free software for statistical computing
Getting help and info
apropos("topic")
The names of all objects in the search list matching the regular expression "topic"
findFn()
Search a database of help packages for functions and return a data.frame
(requires sos package)
Other R references
CRAN task views
Task views provide summaries of R resources for a given domain. Groups of packages can be installed quickly via the ctv package.
Operators
Input and output
read.table(file)
, read.csv(file)
, read.delim(“file”)
, read.fwf(“file”)
Reads a file using defaults sensible for a table/csv/delimited/fixed-width file and creates a data frame from it.
cat(..., file="", sep=" ")
Prints the arguments after coercing to character; sep
is the character separator between arguments
print(a, ...)
Prints its arguments; generic, meaning it can have different methods for different objects
Database I/O
Useful packages: DBI interface between R and relational DBMS; RJDBC access to databases through the JDBC interface; RMySQL interface to MySQL database; RODBC ODBC database access; ROracle Oracle database interface driver; RpgSQL interface to PostgreSQL database; RSQLite SQLite interface for R
Clipboard I/O
File connections of functions can also be used to read and write to the clipboard instead of a file.
Mac OS:
x <‐ read.delim(pipe(“pbpaste”))
Windows:
x <‐ read.delim(ʺclipboardʺ)
See also: read.clipboard
(psych)
Packages
install.packages(“pkgs”, lib)
Download and install pkgs from repository (lib) or other external source
Indexing vectors
Indexing lists
Indexing matrices
Data creation
c(...)
Generic function to combine arguments with the
default forming a vector; with recursive=TRUE
descends through lists combining all elements
into one vector
rep(x,times)
Replicate x
times; use each to repeat
“each” element of x each times; rep(c(1,2,3),2)
is
1 2 3 1 2 3; rep(c(1,2,3),each=2)
is 1 1 2 2 3 3
data.frame(...)
Creates a data frame of the named or
unnamed arguments, e.g.: data.frame(v=1:4, ch=
c("a","B","c","d"), n=10);
Shorter vectors are recycled to the length of the longest
array(x,dim=)
Array with data x
; specify
dimensions like dim=c(3,4,2);
elements of x
recycle if x
is not long enough
gl(n, k, length=n*k, labels=1:n)
Generates levels (factors) by specifying the pattern of their levels;
k
is the number of levels, and n
is the number of
replications
Data conversion
as.array(x)
, as.character(x)
, as.data.frame(x)
,
as.factor(x)
, as.logical(x)
, as.numeric(x)
,
convert type; for a complete list, use
methods(as)