The make-vector function seemed similar to vector in R and printing the initialized results "matrix" produced something that looked like the expected result. (define results (make-vector years (make-vector (vector-length fecundity) 0))) I was trying to make a results matrix out of a vector of vectors.
#For loop in r code code
If you read through the mailing list thread, you would see that this next piece of code was the source of my problem. My R version of the Beverton-Holt function includes 8 parentheses and 2 curly braces. In fact, I tend to overuse them in my R code because I find it more readable. Lisp-family languages have the reputation of being hard to read because of all the parentheses. The Beverton-Holt function definition illustrates Racket's use of prefix operators. Needless to say, I still don't have a good grasp of the similarities and differences between data structures in R and Racket. But Racket also has a list data structure. scalar constantsĪ Racket vector is more like a list in R because it can contain heterogenous data types, e.g., #(1 "cat" #(2 "dog")). #(1 2 3) is equivalent to (vector 1 2 3). The # creates a vector it is shorthand for the vector function. The define function assigns, for example, the value of 30 to the variable years. In the next few lines, we define the parameters used in the model.
#For loop in r code how to
I later discovered that the Racket documentation provides guidance on how to choose between racket and racket/base. In retrospect, it was a strange choice because I liberally use R packages to avoid reinventing the wheel. At the time, I was thinking that was a useful simplification. In this exercise, I chose to work with racket/base rather than the batteries-included racket. At the top of the definitions pane, you need to specify the language because Racket is a programming language laboratory. In DrRacket, the definitions and interactions panes correspond to the source and console panes in RStudio, respectively. Racket comes with an IDE called DrRacket. Now, we will slowly work through my translation of this R code into Racket code. Results = results + beverton_holt(N, surv_age_j, capacity - results) Results = results + beverton_holt(spawn_age_j, fec_age_j * egg_surv, capacity - results) In the results matrix, each row is one year and each column is an age class. The R code follows a conventional imperative approach where an empty matrix is initialized and nested for loops are used to move through the elements of the matrix and propagate the population forward through time. In this example, we consider a hypothetical population with five annual age classes. The simple model used for this exercise is a deterministic multistage Beverton-Holt model. In this post, I recap the mailing list exchange with a target audience of R programmers that are interested in learning more about Racket. I hit a stumbling block and reached out to the helpful folks on the Racket mailing list. For a first task, I tried to build a simple age-structured population model.