The Art of R Programming
8 return(f) 9 } Let’s try this out before going into the internal details: > c1 <- counter() > c2 <- counter() >c ...
7.9 Recursion.................................................................. Once a mathematics PhD student whom I knew to be ...
Note carefully thetermination condition: if (length(x) <= 1) return(x) Without this, the function would keep calling itself r ...
Note that the nature of binary search trees implies that at any node, all of the elements in the node’s left subtree are less th ...
[,1] [,2] [,3] [1,] 2 NA 8 [2,] NA NA 5 [3,] NA NA NA $nxt [1] 3 $inc [1] 3 x <- ins(1,x,6) x $mat [,1] [,2] [,3] [1,] 2 NA ...
[1,]258 [2,]435 [3,] NA NA 6 [4,] NA NA 2 [5,] NA NA 20 [6,] NA NA NA $nxt [1] 6 $inc [1] 3 What happened here? First, the comma ...
[4,] NA NA 2 [5,] NA NA 20 [6,] NA NA NA This represents the tree we graphed for this example. The code follows. Note that it in ...
40 tr$mat <- 41 rbind(tr$mat, matrix(rep(NA,tr$inc*3),nrow=tr$inc,ncol=3)) 42 } 43 # insert new tree node 44 tr$mat[newidx,3] ...
[1] "a" "b" "ab" x abab 124 Consider one line in particular: names(x) <- c("a","b","ab") Looks totally innocuous, eh? Wel ...
[1] 8 88 5 12 13 > x[3] [1] 5 > "["(x,3) [1] 5 > x <- "[<-"(x,2:3,value=99:100) >x [1] 8 99 100 12 13 Again, t ...
6 # construct a new object of class bookvec 7 newbookvec <- function(x) { 8 tmp <- list() 9 tmp$vec <- x # the vector i ...
Our functionnewbookvec()(line 7) does the construction for this class. In it, you can see the structure of the class: An object ...
For instance, we could edit the functionf1()by typing this: > f1 <- edit(f1) This opens the default editor on the code for ...
It instructs R to create a function that adds 1 to its argument and then assigns that function toinc. However, that last step—th ...
8 DOING MATH AND SIMULATIONS IN R R contains built-in functions for your favorite math operations and, of course, for statistica ...
sin(),cos(), and so on: Trig functions min()andmax(): Minimum value and maximum value within a vector which.min()andwhich.max() ...
How does it work? Well, the assignment notp <-1-p creates a vector of all the “not occur” probabilities 1 −pj, using recyclin ...
You can use more than two arguments inpmin(), like this: > pmin(z[1,],z[2,],z[3,]) [1]12 The 1 in the output is the minimum o ...
Here, R reported d dx ex 2 =2xex 2 and ∫ 1 0 x^2 dx≈ 0. 3333333 You can find R packages for differential equations (odesolve), f ...
These functions also have arguments specific to the given distribution families. In our example, we use thedfargument for the ch ...
«
6
7
8
9
10
11
12
13
14
15
»
Free download pdf