Hi, I'm sure this one is very easy.... I am trying to write a function where one of its arguments has two posible (strings) values, defaulting to one of them if none is specified. My problem is that when evaluating the function the following warning is produced: "the condition has length > 1 and only the first element will be used" I've read the help page of if() but I don't get it because when evaluating step by step the warning is not produced. Omitting the details of my function, below is an example of this ## Function fun <- function( n, result = c("simple", "complete") ) { if ( is.null(result) ) result <- "simple" if ( !(result %in% c("simple", "s", "complete", "c")) ) { stop("specify type of result 'simple' or 'complete'") } else { res <- rnorm(n) res } } ## This gives the warning fun(n=20) ## But this not fun(n=20, result="other") ## The warning is not produced when doing this step by step n <- 20 result <- "simple" if ( !(result %in% c("simple", "s", "complete", "c")) ){ stop("specify type of result 'simple' or 'complete'") } else { res <- rnorm(n) res } ## or here n <- 20 result <- "other" if ( !(result %in% c("simple", "s", "complete", "c")) ){ stop("specify type of result 'simple' or 'complete'") } else { res <- rnorm(n) res } Thanks for your time H?ctor -- H?ctor Villalobos <hvillalo at ipn.mx> CICIMAR - IPN A.P. 592. Col. Centro La Paz, Baja California Sur, M?XICO. 23000 Tels. (+52 612) 122 53 44; 123 46 58; 123 47 34 ext. 82425 Fax. (+52 612) 122 53 22
Look at what happens when you don't pass in 'result':> c('simple', 'complete') %in% c("simple", "s", "complete", "c")[1] TRUE TRUE result is a vector of length two and provides two results and the 'if' was only expecting one. You might have to debug your code some more. You probably want to use 'missing' and not 'is.null' 2010/1/19 H?ctor Villalobos <hvillalo at ipn.mx>:> Hi, > > I'm sure this one is very easy.... > > I am trying to write a function where one of its arguments has two posible (strings) values, > defaulting to one of them if none is specified. My problem is that when evaluating the function > the following warning is produced: > > "the condition has length > 1 and only the first element will be used" > > I've read the help page of if() but I don't get it because when evaluating step by step the > warning is not produced. > > > Omitting the details of my function, below is an example of this > > ## Function > fun <- function( n, result = c("simple", "complete") ) > { > ?if ( is.null(result) ) > ? ? result <- ?"simple" > ?if ( !(result %in% c("simple", "s", "complete", "c")) ) { > ? ? ?stop("specify type of result 'simple' or 'complete'") > ?} else { > ?res <- rnorm(n) > ?res > ?} > } > > ## This gives the warning > fun(n=20) > > ## But this not > fun(n=20, result="other") > > ## The warning is not produced when doing this step by step > ?n <- 20 > ?result <- ?"simple" > ?if ( !(result %in% c("simple", "s", "complete", "c")) ){ > ? ? ?stop("specify type of result 'simple' or 'complete'") > ?} else { > ?res <- rnorm(n) > ?res > ?} > > ## or here > ?n <- 20 > ?result <- ?"other" > ?if ( !(result %in% c("simple", "s", "complete", "c")) ){ > ? ? ?stop("specify type of result 'simple' or 'complete'") > ?} else { > ?res <- rnorm(n) > ?res > ?} > > > Thanks for your time > > H?ctor > > > -- > H?ctor Villalobos <hvillalo at ipn.mx> > ?CICIMAR - IPN > ?A.P. 592. Col. Centro > ?La Paz, Baja California Sur, M?XICO. 23000 > ?Tels. (+52 612) 122 53 44; 123 46 58; 123 47 34 ?ext. 82425 > ?Fax. ?(+52 612) 122 53 22 > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Tena koe Hector In addition to Jim's comment, you might like to define your function as: fun <- function(n, result = "simple") This will set the default to 'simple' but one can still call it as fun(result='complete'). HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of H?ctor Villalobos > Sent: Wednesday, 20 January 2010 10:34 a.m. > To: R-help at r-project.org > Subject: [R] coping with a warning in if() > > Hi, > > I'm sure this one is very easy.... > > I am trying to write a function where one of its arguments > has two posible (strings) values, defaulting to one of them > if none is specified. My problem is that when evaluating the > function the following warning is produced: > > "the condition has length > 1 and only the first element will be used" > > I've read the help page of if() but I don't get it because > when evaluating step by step the warning is not produced. > > > Omitting the details of my function, below is an example of this > > ## Function > fun <- function( n, result = c("simple", "complete") ) > { > if ( is.null(result) ) > result <- "simple" > if ( !(result %in% c("simple", "s", "complete", "c")) ) { > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > } > > ## This gives the warning > fun(n=20) > > ## But this not > fun(n=20, result="other") > > ## The warning is not produced when doing this step by step > n <- 20 > result <- "simple" > if ( !(result %in% c("simple", "s", "complete", "c")) ){ > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > > ## or here > n <- 20 > result <- "other" > if ( !(result %in% c("simple", "s", "complete", "c")) ){ > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > > > Thanks for your time > > H?ctor > > > -- > H?ctor Villalobos <hvillalo at ipn.mx> > CICIMAR - IPN > A.P. 592. Col. Centro > La Paz, Baja California Sur, M?XICO. 23000 > Tels. (+52 612) 122 53 44; 123 46 58; 123 47 34 ext. 82425 > Fax. (+52 612) 122 53 22 > > ______________________________________________ > 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. >
H?ctor Villalobos <hvillalo <at> ipn.mx> writes:> > Hi, > > I'm sure this one is very easy.... > > I am trying to write a function where one of its arguments has two posible (strings) values, > defaulting to one of them if none is specified. My problem is that when evaluating the function > the following warning is produced: > > "the condition has length > 1 and only the first element will be used" > > I've read the help page of if() but I don't get it because when evaluating step by step the > warning is not produced. > > Omitting the details of my function, below is an example of this > > ## Function > fun <- function( n, result = c("simple", "complete") ) > { > if ( is.null(result) ) > result <- "simple" > if ( !(result %in% c("simple", "s", "complete", "c")) ) { > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > }Take a look at ?match.arg for one solution. fun <- function( n, result = c("simple", "complete") ) { result <- match.arg(result) res <- rnorm(n) res } fun(n=20) fun(n=20, result="simple") fun(n=20, result="comp") fun(n=20, result="something") Mark Lyman
Thanks to all who responded. I've learn a lot from your comments. The best solution I can see is to use both 'missing()' and 'match.arg()' in my function. Héctor> Hi, > > I'm sure this one is very easy.... > > I am trying to write a function where one of its arguments has two posible (strings) values, > defaulting to one of them if none is specified. My problem is that when evaluating the function > the following warning is produced: > > "the condition has length > 1 and only the first element will be used" > > I've read the help page of if() but I don't get it because when evaluating step by step the > warning is not produced. > > > Omitting the details of my function, below is an example of this > > ## Function > fun <- function( n, result = c("simple", "complete") ) > { > if ( is.null(result) ) > result <- "simple" > if ( !(result %in% c("simple", "s", "complete", "c")) ) { > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > } > > ## This gives the warning > fun(n=20) > > ## But this not > fun(n=20, result="other") > > ## The warning is not produced when doing this step by step > n <- 20 > result <- "simple" > if ( !(result %in% c("simple", "s", "complete", "c")) ){ > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > > ## or here > n <- 20 > result <- "other" > if ( !(result %in% c("simple", "s", "complete", "c")) ){ > stop("specify type of result 'simple' or 'complete'") > } else { > res <- rnorm(n) > res > } > > > Thanks for your time > > Héctor[[alternative HTML version deleted]]