Skip to content

Instantly share code, notes, and snippets.

@rpietro
Created August 16, 2013 04:18
Show Gist options
  • Save rpietro/6247296 to your computer and use it in GitHub Desktop.
Save rpietro/6247296 to your computer and use it in GitHub Desktop.
Hadley's vocabulary with additional definitions and reproducible examples
# Hadley's vocabulary with additional definitions and reproducible examples
This document takes the original [Vocabulary chapter](https://github.com/hadley/devtools/wiki/Vocabulary) from Hadley Wickham's in progress book "[Advanced R development: making reusable code](https://github.com/hadley/devtools/wiki)" and adds definitions and reproducible examples using [knitr](http://yihui.name/knitr/). Unless otherwise noted, the examples come straight from the help for each function or are modified versions from those examples.
```{r}
library("knitr")
```
##The basics
### The first functions to learn
`?` help on functions and datasets
```{r}
?cat
```
`str` displays the internal str_ucture of an R object
```{r}
a <- c(1:10)
str(a)
b <- c("a", "b", "c")
str(b)
```
### Important operators and assignment
`%in%` demonstrates which elements of the vector on the right match the vector on the left. this function is similar to `match` but with a different syntax
```{r}
1:10 %in% c(1,3,5,9)
sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","%")
sstr[sstr %in% c(letters, LETTERS)]
```
`match` returns a vector of the positions of (first) matches of its first argument in its second
a <- c(1:10)
b <- c(7:20)
match(a, b)
`=` indicates an assignment
```{r}
a = 1
a
```
`<-` indicates an assignment
```{r}
a <- 1
a
```
`<<-` used in functions to search through the environment for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment
```{r}
a <<- 1
a
```
`$`
`[`
`[[`
`head`
`tail`
`subset`
`with`
`assign`
`get`
### Comparison
`all`
`equal`
`identical`
`!=`
`==`
`>`
`>=`
`<`
`<=`
`is.na`
`complete.cases`
`is.finite`
### Basic math
`*`
`+`
`-`
`/`
^
%%
%/%
abs
sign
acos
asin
atan
atan2
sin
cos
tan
ceiling
floor
round
trunc
signif
exp
log
log10
log2
sqrt
max
min
prod
sum
cummax
cummin
cumprod
cumsum
diff
pmax
pmin
range
mean
median
cor
sd
var
rle
### Functions
function
missing
on.exit
return
invisible
### Logical & sets
&
|
!
xor
all
any
intersect
union
setdiff
setequal
which
### Vectors and matrices
c
matrix
### automatic coercion rules character > numeric > logical
length
dim
ncol
nrow
cbind
rbind
names
colnames
rownames
t
diag
sweep
as.matrix
data.matrix
### Making vectors
c
rep
rep_len
seq
seq_len, seq_along
rev
sample
choose, factorial, combn
(is/as).(character/numeric/logical/...)
### Lists & data.frames
list, unlist
data.frame, as.data.frame
split
expand.grid
### Control flow
if, &&, || (short circuiting)
for, while
next, break
switch
ifelse
## Common data structures
### Date time
ISOdate
ISOdatetime
strftime
strptime
date
difftime
julian
months
quarters
weekdays
library(lubridate)
### Character manipulation
grep
agrep
gsub
strsplit
chartr
nchar
tolower
toupper
substr
paste
library(stringr)
### Factors
factor
levels
nlevels
reorder
relevel
cut
findInterval
interaction
options(stringsAsFactors = FALSE)
### Array manipulation
array
dim
dimnames
aperm
library(abind)
## Statistics
### Ordering and tabulating
duplicated
unique
merge
order
rank
quantile
sort
table
ftable
### Linear models
fitted
predict
resid
rstandard
lm
glm
hat
influence.measures
logLik
df
deviance
formula
~
I
anova
coef
confint
vcov
contrasts
### Miscellaneous tests
apropos("\\.test$")
### Random variables
(q, t, d, r) * (beta, binom, cauchy, chisq, exp, f, gamma, geom,
hyper, lnorm, logis, multinom, nbinom, norm, pois, signrank, t,
unif, weibull, wilcox, birthday, tukey)
### Matrix algebra
crossprod
tcrossprod
eigen
qr
svd
%*%
%o%
outer
rcond
solve
## Working with R
### Workspace
ls
exists
rm
getwd
setwd
q
source
install.packages
library calls a package
```{r}
library("ggplot2")
```
require is identical to library, but it tends to be used inside functions since it will give you a warning and continue if the package is not found. In contrast, library will give you an error
```{r}
require("ggplot2")
```
### Help
help
? <!-- this is redundant -->
help.search
apropos
RSiteSearch
citation
demo
example
vignette
### Debugging
traceback
browser
recover
options(error = )
stop
warning
message
tryCatch
try
I/O
### Output
print
cat
message
warning
dput
format
sink
capture.output
### Reading and writing data
data
count.fields
read.csv
write.csv,
read.delim
write.delim
read.fwf
readLines
writeLines
readRDS
saveRDS
load
save
library(foreign)
### Files and directories
dir
basename, dirname, tools::file_ext
file.path
path.expand, normalizePath
file.choose
file.copy, file.create, file.remove, file.rename, dir.create
file.exists, file.info
tempdir, tempfile
download.file, library(downloader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment