100% found this document useful (1 vote)
41 views2 pages

Peter Adler RCheat Sheet

This document provides a concise list of useful R functions organized into categories like input/output, data manipulation, indexing, database operations, and plotting. It was written by Peter Adler in 2005 as an introduction to common and helpful R functions for analysis. Key functions are listed with brief descriptions. For more details on any function, the reader is directed to consult the R help system.

Uploaded by

Bassic_Instinct
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
41 views2 pages

Peter Adler RCheat Sheet

This document provides a concise list of useful R functions organized into categories like input/output, data manipulation, indexing, database operations, and plotting. It was written by Peter Adler in 2005 as an introduction to common and helpful R functions for analysis. Key functions are listed with brief descriptions. For more details on any function, the reader is directed to consult the R help system.

Uploaded by

Bassic_Instinct
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

February 2005: Peter Adler: NCEAS Postdoctoral Fellow and Power R user, provides

this concise list of useful R functions.


For further details concerning any of these functions, consult the R Language help system
by entering ?<function_name> at the R system prompt (example: >?read.table)
These Are a Few of My Favorite R Things

Input/Output:
read.table() Read in many kinds of data
read.csv() Read in comma delimited data
write.table() Write tabular data in many formats
scan() Read in really huge datasets (faster but more complicated function)
getwd() See the current working directory
setwd() Set the current working directory

Create and manipulate data objects:


c() Make a vector of specified elements, or add to a vector
seq() Numeric sequences
rep() Vectors of repeated values
matrix() 2-dimensional arrays
array() Mulitdimensional arrays
data.frame() Looks like a matrix, but some columns can be numeric, others character
(actually this is a special type of list)
list() Collections of different data objects (“cell arrays” in Matlab)
rm() Remove (erase) objects
ls() Print a list of the all objects currently in memory

Indexing:
which() Find elements of a vector that satisfy a condition
is.element() Given two vectors, find elements that occur in both
length() Get the length of a vector
dim() Get or assign the dimensions of a matrix, dataframe, or array
ncol() The number of columns in a matrix
nrow() The number of rows in a matrix
colnames() Get or assign column names of a vector
names() Get or assign the column names of a dataframe or object names of a list

Database operations:
merge() Merge or join dataframes
unique() List of each unique element in a vector (even if they repeat)
subset() Pull out certain records in a dataframe
aggregate() Do operations (means, sums) on columns of dataframe given grouping
variables
stack() Take repeated elements of a vector and put them in different colums
reshape() Like stack() and unstuck() but more flexible
sort() Sort a vector
table() Count the number of times individual values occur

Take sums/means/variances of rows/columns/arrays:


rowSums() Just what it sounds like for 2-D matrices
colSums() Ditto
apply() Apply any function to specified dimensions of an array

Control structures:
for() Loops
break() Escape a for loop
if(), else() Self-explanatory

Probability:
runif() Draw random numbers from a uniform distribution
rnorm() Draw random numbers from a normal distribution
dnorm() Normal density function (many other distributions available)
sample() Sample elements from a vector

Text manipulation:
paste() Concatenate strings
grep() Find regular expressions in a character vector
substr() Extract or replace pieces of a character vector

General plotting functions:


plot() Scatter and line plots
matplot() Literally “matrix plot”, good for time series of many responses
hist() Histograms
barplot() Barplots
image() Good for “maps” of matrices
axis() Create a “custom” plot axis
text() Write text in a plot
mtext() Write text in the margins of a plot
pdf() Create the figure as a pdf file (also jpeg, postrscript, etc)

My favorite arguments within par(), the graphics parameters list:


mfrow() Create a multipanel plot
new Add a new plot to an existing plot
mar() Lines of space in margin of each plotting panel
oma() Lines of space in the outer margin of the plot
tcl() Length of axis tick marks
cex Magnification of symbols and labels (also see cex.axis(), etc)
mgp() Controls distance between the axis and axis labels and title

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy