Första bekantskapen med Hedleys bok

Jag utgår här från Hadleys bok:
https://github.com/hadley/devtools/wiki/

Detta är samtidigt också ett litet test för att testa versionshantering med Git!

Intro

https://github.com/hadley/devtools/wiki/Introduction

Atomic vectors are logical, integer, numeric, character and raw.

x <- 1:10
mode(x)  # = numeric
## [1] "numeric"
length(x)
## [1] 10
names(x)
## NULL

names(x) <- letters[1:10]
x
##  a  b  c  d  e  f  g  h  i  j 
##  1  2  3  4  5  6  7  8  9 10 
names(x)
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

x <- list(list(list(list())))
x
## [[1]]
## [[1]][[1]]
## [[1]][[1]][[1]]
## list()
## 
## 
## 
str(x)
## List of 1
##  $ :List of 1
##   ..$ :List of 1
##   .. ..$ : list()

Matriser

y <- matrix(1:20, nrow = 4, ncol = 5)
z <- array(1:24, dim = c(3, 4, 5))

nrow(y)
## [1] 4
rownames(y)
## NULL
ncol(y)
## [1] 5
colnames(y)
## NULL

dim(z)
## [1] 3 4 5
dimnames(z)
## NULL

attributes bra kommando för att se all info om objekt!

Functions

f <- function(x) {
    x$a <- 2
}
x <- list(a = 1)
f()
## Error: argument "x" is missing, with no default
x$a
## [1] 1

Infix function

"%+%" <- function(a, b) paste(a, b)
"new" %+% "string"
## [1] "new string"

Replacement functions

"second<-" <- function(x, value) {
    x[2] <- value
    x
}
x <- 1:10
second(x) <- 5
x
##  [1]  1  5  3  4  5  6  7  8  9 10

Vocabulary

Operators

%in%, match
=, <-, <<-, assign
$, [, [[, replace, head, tail, subset
with
within

Comparison

all.equal, identical
!=, ==, >, >=, <, <=
is.na, is.nan, is.finite
complete.cases

Basic math

*, +, -, /, , %%, %/%
abs, sign
acos, acosh, asin, asinh, atan, atan2, atanh
sin, sinh, cos, cosh, tan, tanh
ceiling, floor, round, trunc, signif
exp, log, log10, log1p, log2, logb, sqrt

cummax, cummin, cumprod, cumsum, diff
max, min, prod, sum
range
mean, median, cor, cov, sd, var
pmax, pmin
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, seq, seq_along, seq_len
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

Statistics

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

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

Ordering and tabulating

duplicated, unique
merge
order, rank, quantile
sort
table, ftable
Working with R

Workspace

ls, exists, get, rm
getwd, setwd
q
source
install.packages, library, require

Help

help, ?
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

Reading and writing data

data
count.fields
read.csv, read.delim, read.fwf, read.table
library(foreign)
write.table
readLines, writeLines
load, save
readRDS, saveRDS

Files and directories

dir
basename, dirname, file.path, path.expand
file.choose
file.copy, file.create, file.remove, file.rename, dir.create
file.exists
tempdir, tempfile
download.file
Special data

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)


LÄXAN

get är kommando för Return the value of a named object