Peter Adler RCheat Sheet
Peter Adler RCheat Sheet
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
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
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