[R] R function returning a list of variable(s) conditional on the value of an option?

David McPearson dmcp at webmail.co.za
Fri Jul 25 15:44:38 CEST 2014


I don't know about better or more elegant - but see inserts below...

Cheers.

On Fri, 25 Jul 2014 22:22:56 +1000 "Luke Hartigan" <luke.hartigan at bigpond.com>
wrote

> Dear all,
> 
> I have an R function which returns a list of variables; however, within the
> body of the function I would like to incorporate a branch based on a user
> selected option that will mean there will be one different variable to
> return based on the option value.
> 
> I was thinking of doing something like this (only an example):
> 
> foo <- function(x, y, option = TRUE)
> {
>     .
      rtn <- list(x = x, y = y)
>     If (option == TRUE) {
>     #    z = z
          rtn$z <- z
          return(rtn)
>     } else {
>     #    w = w
           rtn$w <- w
           return(rtn)
> }
> #    return(list(x = x, y = y, z = ifelse(option == TRUE, z, NA), w =
> # ifelse(option != TRUE, w, NA))
>     
> }
> 
> However, I was wondering if anyone else had a better idea/method which might
> be more elegant?
> 
> Many thanks,
> Luke
> 
> 
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



____________________________________________________________
South Africas premier free email service - www.webmail.co.za 

Cheapest Insurance Quotes!
https://www.outsurance.co.za/insurance-quote/personal/?source=msn&cr=Postit14_468x60_gif&cid=322



More information about the R-help mailing list