R (15) Admin (12) programming (11) Rant (6) personal (6) parallelism (4) HPC (3) git (3) linux (3) rstudio (3) spectrum (3) C++ (2) Modeling (2) Rcpp (2) SQL (2) amazon (2) cloud (2) frequency (2) math (2) performance (2) plotting (2) postgresql (2) DNS (1) Egypt (1) Future (1) Knoxville (1) LVM (1) Music (1) Politics (1) Python (1) RAID (1) Reproducible Research (1) animation (1) audio (1) aws (1) data (1) economics (1) graphing (1) hardware (1)

19 May 2009

Adventures in R Recursion

Spent the day on the coffee shop patio fielding OLPC questions - from "Wow, that's cool, what is it?" to "Where can i get one?" I demonstratively poured some tea on the keyboard at one point for waterproof emphasis to "coooool"s. Good times. Installed R on the green machine (which is now runing happy-fun-times modified ubuntu. Spent time working out of Venables and Ripley's "S Programming" book - trying to keep some finals week brain momentum going... Plunking example code into the green machine. I kept the screen backlight off, just using sunlight and low battery while the sun was up. When i took a break from coding, i could hit the power button to put it to sleep. So, i worked through some recursion examples, in the meantime discovering my own knowledge-gaps in indexing ( this[-index] is the compliment of this[index] for a numeric index vector, whereas this[!index] is the compliment for a logical index vector) and list packing. Also, found a test example of "=" not the same as "<-" a=1:10 > index1 = a>2 ; any(index2<-(a>2)) ; identical(index1, index2) [1] TRUE [1] TRUE > index1 = a>5 ; any(index2=(a>5)) ; identical(index1, index2) [1] TRUE [1] FALSE ### index2 assignment isn't made... why?! I find recursion confusing; debugging recursive functions is especially so! The fruits of my labor are here, with examples of all of the above... mk.recur = function(a,lim=5, fac=2){ ## takes a numeric vector, returns a list of numeric vectors ## with elements greater than lim, multiplied by fac recursively if(any(index<-a>lim)) {ret=list(a[index])} if(length(a[!index])==0) {return(ret)} ret=append(ret, Recall(fac*a[!index], lim, fac)) return(ret) }

No comments:

Post a Comment