My obj does not always come with a logical variable defined. So I do my.foo <- function(obj,df,digits=5){ if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher ... } This works when "Fisher" is defined in/passed from obj. When it is not, I get error: Error in (!is.na("obj$spec$Fisher")) & Fisher : operations are possible only for numeric, logical or complex types I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks.
Hi, Does this work for you? It simply tests if the name Fisher is found among the names of the elements of spec. obj = list(spec = list) Fisher <- ifelse(!("Fisher" %in% names(obj$spec)), FALSE, obj$spec$Fisher) Cheers, Ben On Dec 14, 2014, at 8:07 AM, Steven Yen <syen04 at gmail.com> wrote:> My obj does not always come with a logical variable defined. So I do > > my.foo <- function(obj,df,digits=5){ > if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher > ... > } > > This works when "Fisher" is defined in/passed from obj. When it is not, I get error: > > Error in (!is.na("obj$spec$Fisher")) & Fisher : > operations are possible only for numeric, logical or complex types > > I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org
On 14/12/2014, 8:07 AM, Steven Yen wrote:> My obj does not always come with a logical variable defined. So I do > > my.foo <- function(obj,df,digits=5){ > if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher > ... > } > > This works when "Fisher" is defined in/passed from obj. When it is > not, I get error: > > Error in (!is.na("obj$spec$Fisher")) & Fisher : > operations are possible only for numeric, logical or complex types > > I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks.The test you want is based on is.null(), and you definitely don't want quotes there. For example, if (!is.null(obj$spec) && !is.null(obj$spec$Fisher)) Fisher <- obj$spec$Fisher Duncan Murdoch
Thanks. This worked!! :) Fisher <- ifelse(!("Fisher" %in% names(obj$spec)), FALSE, obj$spec$Fisher) Steven At 08:43 AM 12/14/2014, Ben Tupper wrote:>Hi, > >Does this work for you? It simply tests if the name Fisher is found >among the names of the elements of spec. > >obj = list(spec = list) >Fisher <- ifelse(!("Fisher" %in% names(obj$spec)), FALSE, obj$spec$Fisher) > >Cheers, >Ben > >On Dec 14, 2014, at 8:07 AM, Steven Yen <syen04 at gmail.com> wrote: > > > My obj does not always come with a logical variable defined. So I do > > > > my.foo <- function(obj,df,digits=5){ > > if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher > > ... > > } > > > > This works when "Fisher" is defined in/passed from obj. When it > is not, I get error: > > > > Error in (!is.na("obj$spec$Fisher")) & Fisher : > > operations are possible only for numeric, logical or complex types > > > > I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks. > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > >Ben Tupper >Bigelow Laboratory for Ocean Sciences >60 Bigelow Drive, P.O. Box 380 >East Boothbay, Maine 04544 >http://www.bigelow.org