This page contains listings of some R functions and commands by category.
Workspace and help
Workspace and help
function name
|
functionality
|
getwd()
|
get working directory
|
setwd(dir)
|
set working directory to dir
|
help(topic), ?topic
|
get help on topic, usually function name
|
help.search("keyword")
|
search for help
|
help.start()
|
HTML help start page
|
demo()
|
list available demonstrations
|
save(..., file), load(file)
|
save and load objects
|
savehistory(file), loadhistory(file)
|
save and load command history
|
source(file)
|
execute commands from file
|
list.files(dir), dir(dir)
|
list files in directory dir
|
q()
|
quit R
|
Objects
Objects
function name
|
functionality
|
mode(x)
|
mode of x
|
ls(), objects()
|
list objects in memory
|
rm(x), rm(list = ls())
|
remove x or all objects
|
exists(x)
|
test if object x exists in memory
|
as.numeric(x), as.list(x), ...
|
coerce mode of object x
|
is.numeric(x), is.na(x), ...
|
test mode of object x
|
identical(x, y)
|
test if objects are identical
|
return(invisible(x))
|
return invisible copy (doesn't print)
|
Packages
Packages
function name
|
functionality
|
install.packages(name)
|
download and install package name
|
download.packages(name, dir)
|
download package name into dir
|
library(name), require(name)
|
load package name
|
data(name)
|
load dataset name
|
.libPaths(dir)
|
add directory dir to library paths
|
sessionInfo()
|
list loaded packages
|
Flow and control and function definition
Flow and control and function definition
function name
|
functionality
|
if(logical_expression) expression_1 else expression_2
|
if then else
|
for (x in vector) expression
|
loop through elements of vector as x
|
while (logical_expression) expression
|
while loop
|
name <- function(input1, ...) {expression_1; ...; return(output))
|
function definition
|
stop(message)
|
cease processing and print message
|
browser()
|
stop to inspect objects for debugging
|
system.time(expression)
|
report runtime for expression
|
Mathematical and logical operators and functions
Mathematical and logical operators and functions
function name
|
functionality
|
+, -, *, /, ^, %%, %/%
|
algebraic operators, %% for remainder of integer division, %/% for integer division i.e. rounded down to 0 decimal places
|
<, >, <=, >=, ==, !=
|
comparison operators
|
, !
|
logical operators (and, or, not)
|
&&, |
|
and/or evaluated progressively from left
|
xor(A, B)
|
exclusive or (A or B but not both)
|
ifelse(condition, x, y)
|
choose x or y elementwise
|
sin(x), cos(x), tan(x)
|
sine, cosine and tangent
|
asin(x), acos(x), atan(x)
|
inverse sine, cosine and tangent
|
exp(x), log(x)
|
exponential and logarithm base e
|
sqrt(x)
|
square root
|
abs(x)
|
absolute value
|
pi
|
3.145926...
|
ceiling(x)
|
smallest integer >= x
|
floor
|
largest integer <= x
|
all.equal(x, y)
|
almost equal
|
round(x, k)
|
round x to k digits
|
deriv(expression, vars)
|
symbolic differentiation
|
Vectors
Vectors
function name
|
functionality
|
x[i]
|
select subvector using index vector
|
x[logical], subset(x, subset)
|
select subvector using logical vector
|
c(...)
|
combine vectors
|
seq(from, to, by), from:to
|
generate an arithmetic sequence
|
rep(x, times)
|
generate repeated values
|
length(x)
|
length of x
|
which(x)
|
indices of TRUE elements of x
|
sum(...)
|
sum over vector(s)
|
prod(...)
|
product over vector(s)
|
cumsum(x), cumprod(x)
|
cumulative sum and product
|
min(...), max(...)
|
minimum and maximum
|
sort(x)
|
sort a vector
|
mean(x)
|
sample mean
|
var(x), sd(x)
|
sample variance and standard deviation
|
order(x)
|
rank order of elements of x
|
Statistical tests