[R] get element of list with default?

William Dunlap wdunlap at tibco.com
Tue Apr 15 23:08:49 CEST 2014


I think the following is a trifle faster than the i%in%names(x) version,
probably because it only does the name lookup once.  (I also don't
like to use return() - it is too much like goto.)

getElement3 <- function(x, i, default) {
   i <- match(i, names(x))
   if (is.na(i)) {
      default
   } else {
      x[[i]]
   }
}

Like the others, it does not check the assumption that 'i' is a character
vector of length 1.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Spencer Graves
> Sent: Tuesday, April 15, 2014 1:05 PM
> To: Hadley Wickham
> Cc: R list
> Subject: Re: [R] get element of list with default?
> 
> Thanks to Marc Schwartz and Hadley Wickham:
> 
> 
>        Based on their comments, I think I'll add a combination of
> Hadley's code and getElement{base} Ecfun in a form like
> getElement2(object, name, default).
> 
> 
>        Best Wishes,
>        Spencer
> 
> 
> On 4/15/2014 9:33 AM, Hadley Wickham wrote:
> > You really want to use the names of the list since lists can contain
> > null.  I'd recommend something more like:
> >
> > getElement <- function(x, i, default) {
> >    if (i %in% names(x)) return(x[[i]])
> >    default
> > }
> >
> > Hadley
> >
> > On Tue, Apr 15, 2014 at 10:53 AM, Spencer Graves
> > <spencer.graves at structuremonitoring.com> wrote:
> >> 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
> >>      }
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> >
> 
> 
> --
> Spencer Graves, PE, PhD
> President and Chief Technology Officer
> Structure Inspection and Monitoring, Inc.
> 751 Emerson Ct.
> San José, CA 95126
> ph:  408-655-4567
> web:  www.structuremonitoring.com
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list