Vectors
integer()
, character()
, numeric()
, logical()
, raw()
, complex()
x = rnorm(1000)
y = x + rnorm(sd=.5, 1000)
[
- single bracket subset; 'endomorphism'length()
c()
[<-
– subset-assignnames()
)functions: argument names;
sd
; can be partial, e.g., s=
) – matched before unnamedpositional – unnamed are matched by position
rnorms = lapply(0:3, function(mean) {
rnorm(1000, mean)
})
rnorms = lapply(0:3, rnorm, n=1000, mean=0)
matrix()
atomic vectors with 'dim' and 'class' attributes
'API' – two- (n-) dimensional [
, [<-
m = matrix(1:6, 2)
dput(m)
## structure(1:6, .Dim = 2:3)
factor()
– decorated integer() vector
list()
[[
, $
– extract element of list[[<-
, $<-
– assign new elementunlist()
)data.frame()
closures
acctFactory = function() {
balance <- 0
list(deposit=function(amt) {
balance <<- balance + amt
}, currBalance=function() {
balance
})
}
x = rnorm(1000)
y = x + rnorm(sd=.5, 1000)
df = data.frame(X=x, Y=y)
Use of data.frame()
:
data.frame()
'contract'fit = lm(Y ~ X, df)
plot(Y ~ X, df)
abline(fit, lwd=4, col="red")
anova(fit)
## Analysis of Variance Table
##
## Response: Y
## Df Sum Sq Mean Sq F value Pr(>F)
## X 1 1018.73 1018.73 4098.4 < 2.2e-16 ***
## Residuals 998 248.07 0.25
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
fit
is an S3 object (instance, class)
list()
with a class
attributeclass()
to discover the class(!)anova
is a generic, with a method appropriate for the class of fit
?plot
(for the generic), ?plot.lm
(for the method)suppressPackageStartupMessages({
library(IRanges)
})
start <- as.integer(runif(1000, 1, 1e4))
width <- as.integer(runif(length(start), 50, 100))
ir <- IRanges(start, width=width)
coverage(ir)
## integer-Rle of length 10092 with 1723 runs
## Lengths: 34 5 4 8 3 1 8 5 6 16 ... 3 1 7 7 14 1 4 25 15 17
## Values : 1 2 3 4 5 6 5 6 7 8 ... 10 9 8 7 6 5 4 3 2 1
S4 is more formal than S3
discovery
class(ir)
; could look at (but why bother?) structure using getClass(class(ir))
showMethods("coverage")
,
showMethods(class=class(ir), where=search())
help
?coverage
– help on the generic?IRanges
– Constructor; recent convention: also documents class & important methodsselectMethod("coverage", signature=class(ir))
to figure out method dispatch, and to see the function definitionmethod?"coverage,Ranges"
(tab completion!)class?IRanges
(tab completion!)Sequences
DNAString
, DNAStringSet
Ranges
GRanges
, GRangesList
Integrated containers
SummarizedExperiment
Brief review of lecture material
Efficient R
code
compiler::cmpfun()
) surprisingly effective at improving
f1()
– better than sapply()
.vapply()
. Faster and safer than sapply()
, so should be
a best practiceR
code; makes appeal
to C++ / parallel evaluation less compellingreduceByYield()
to iterate through filesbplapply()
.bplapply()
output. :(Brief review of lecture material
General importance of select()
interface, including to on-line
resources such as biomaRt
Very easy to wrangle web-based genome annotation files, e.g., UCSC chain files
Role of AnnotationHub in deploying more complicated and heavily curated resources, like the GRASP2 data base of GWAS variants