Run Ctrl+Shift+Enter. Insert Chunk Ctrl+Alt+I.
exactlyone <- function(p) {
notp <- 1 - p
tot <- 0.0
for (i in 1:length(p))
tot <- tot + p[i] * prod(notp[-i])
return(tot)
}
x <- c(25,55,133)
cumsum(x) #to return cumulative sums
## [1] 25 80 213
cumprod(x) #to return cumulative products
## [1] 25 1375 182875
nlm(function(x) return(x^2-sin(x)),8)
## $minimum
## [1] -0.2324656
##
## $estimate
## [1] 0.4501831
##
## $gradient
## [1] 4.024558e-09
##
## $code
## [1] 1
##
## $iterations
## [1] 5
nlm(function(x) return(3*2-cos(x)),8)
## $minimum
## [1] 5
##
## $estimate
## [1] 6.283182
##
## $gradient
## [1] -1.176099e-07
##
## $code
## [1] 1
##
## $iterations
## [1] 4
D(expression(exp(x^2)),"x") # derivative
## exp(x^2) * (2 * x)
D(expression(exp(x^3)+x),"x") # derivative
## exp(x^3) * (3 * x^2) + 1
integrate(function(x) x^2,0,1)
## 0.3333333 with absolute error < 3.7e-15
x1 <- c(2,20,200)
y1 <- c(50,200,2,90)
8.1. combine unique values
union(x1,y1)
## [1] 2 20 200 50 90
8.2. select which intersect in both x1 & y1
intersect(x1,y1)
## [1] 2 200
8.3. select which of x1 not part of y1
setdiff(x1,y1)
## [1] 20
8.4. select which of y1 not part of x1
setdiff(y1,x1)
## [1] 50 90