recursion - Recursively editing a list in R -


in program, recursively going on nested list , adding elements overall list return. there few details taken care of, can't use unlist.

formulapart taken formula object.

my code is:

parsevariables <- function(formulapart, mylist){      for(currentvar in as.list(formulapart))        if(typeof(currentvar == 'language'          parsevariables(currentvar, mylist)        else          if(! tostring(currentvar) %in% c(\\various characters)            list <- c(list, currentvar)     } 

i have checked function correctly adds elements list when should. problem list loses elements due recursion. elements added during 1 inner recursive call not saved recursive call.

if in c++, use pointer; same java. however, not understand how handle error in r.

r pass-by-value, can't modify (most) existing objects passing them function. if want add on recursively, 1 trick use environment instead, passed reference. can coerced list when you're done.

parsevariables <- function(formulapart, mylist){      for(currentvar in as.list(formulapart)) {        if(typeof(currentvar) == 'language') {          parsevariables(currentvar, mylist)        }        else {          if(! tostring(currentvar) %in% c(':', '+', '~'))            assign(tostring(currentvar), currentvar, mylist)         }     } }  f1 <- z ~ a:b + x f2 <- z ~ x + y  mylist <- new.env()  parsevariables(f1, mylist) parsevariables(f2, mylist) ls(mylist) # [1] "a" "b" "x" "z" as.list(mylist) # $x # x #  # $z # z #  # $a # #  # $b # b 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -