[R] get element of list with default?

Spencer Graves spencer.graves at structuremonitoring.com
Tue Apr 15 17:53:15 CEST 2014


Hello:


       Do you know of a simple function to return the value of a named 
element of a list if that exists, and return a default value otherwise?


       It's an easy function to write (e.g., below).  I plan to add this 
to the "Ecfun" package unless I find it in another CRAN package.


       Thanks,
       Spencer


     getElement <- function(element, default, list){
#       get element of list;  return elDefault if absent
         El <- list[[element]]
         if(is.null(El)){
             El <- default
         }
         El
     }




More information about the R-help mailing list