- math
- x<c(12,5,13)
- cumsum(x)
- 12 17 30
- cumprod(x) # mul
- 12 60 780
- z
- 1 2
- 5 3
- 6 2
- min(z[,1],z[,2])
- 1
- pmin(z[,1],z[,2]) # by row
- 1 3 2
- pmin(z[1,],z[2,],z[3,]) # by col
- 1 2
- nlm(function(x) return(x^2-sin(x)),8)
- minimum # -0.23
- estimate # 0.45 when x=0.45 functions's minimum is -0.23
- iterations # 5 using Newton-Raphson method
- calculus
- D(expression(exp(x^2)),"x") # derivative
- exp(x^2)*(2*x)
- integrate(function(x) x^2,0,1)
- 0.333333 with aboslute error < 3.7e-15
- distribution
- dnorm dchiq dbinom
- pnorm pchisq pbinom
- qnorm qchisq qbinom
- rnorm rchisq rbinom
- mean(rchisq(1000,df=2))
- 1,938179
- qchisq(0.95,2)
- 5.991465
- qchisq(c(0.5,0.95),df=2)
- 1.386294 5.991465
- rank
- y
- V1 V2
- def 2
- ab 5
- zzzz 1
- r<-order(y$V2) # 3 1 2
- z<-y[r,]
- z
- V1 V2
- zzzz 1
- def 2
- ab 5
- d
- kids ages
- Jack 12
- Jill 10
- Billy 13
- d[order(d$kids),]
- kids ages
- Billy 13
- Jack 12
- Jill 10
- d[order(d$ages),]
- kids ages
- Jill 10
- Jack 12
- Billy 13
- x<-c(13,5,12,5)
- rank(x) # 4.0 1.5 3.0 1.5 5 occurs twice with rank 1 and 2, so mean is 1.5
- algebra
- crossprod(1:3,c(5,12,13)) # 68=1*5+2*12+3*13, dot product
- a<-matrix(c(1,1,-1,1),nrow=2,ncol=2)
- b<-c(2,4)
- solve(a,b) # solve equation x1+x2=2 -x1+x2=4
- 3 1
- solve(a) # inverse matrix
- 0.5 0.5
- -0.5 0.5
- m
- 1 2
- 7 8
- dm<-diag(m)
- dm
- 1 8
- diag(m)
- 1 0
- 0 8
- diag(3)
- 1 0 0
- 0 1 0
- 0 0 1
- m
- 1 2 3
- 4 5 6
- 7 8 9
- sweep(m,1,c(1,4,7),"+") # 1 means by row
- 2 3 4
- 8 9 10
- 14 15 16
- set
- x<-c(1,2,5)
- y<-c(5,1,8,9)
- union(x,y) # 1 2 5 8 9
- intersect(x,y) # 1 5
- setdiff(x,y) # 2 in x but not in y
- setdiff(y,x) # 8 9 in y but not in x
- setequal(x,y) # FALSE
- setequal(x,c(1,2,5)) # TRUE
- 2%in%x # TRUE
- 2%in%y #FALSE
- choose(5,2) # 10 subset number like combination(5,2)
- symdiff<-function(a,b) {
- a<-setdiff(x,y)
- b<-setdiff(y,x)
- return(union(a,b))
- }
- "%subsetof%"<-function(u,v) { # is u a subset of v
- return(setequal(intersect(u,v),u))
- }
- c(3,8)%subsetof%1:10 # TRUE
- c(3,8)%subsetof%5:10 # FALSE
- combn(1:3,2,sum) # 3 4 5 all 2-length subset's sum {1,2} {1,3} {2,3}
- simulation
- x<-rbinom(10000,5,0.5) # 10000 random variables
- mean(x>=4) # 0.18829
- sum<-0
- nreps<-100000
- for (i in 1:nreps) {
- xy<-rnorm(2) # 2 N(0,1)s
- sum<-sum+max(xy)
- }
- print(sum/nreps)
- emax<-function(nreps) {
- x<-rnorm(2*nreps)
- maxxy<-pmax(x[1:nreps],x[(nreps+1):(2*nreps)])
- return(mean(maxxy))
- }
- set.seed(8888)