Hi - I was wondering if anyone came across a problem such as mine! I was trying to create a user-defined function, however, the results from calling the function are unexpected! When passing X parameter as a single value variable (x<-c(3)), everything works fine. However, when passing a parameter as a vector with multiple values (as the case in my code), the 'if condition' is only executed once! Here is my code:> x[1] 0.3 1.0 0.7 22.0> myfunction<-function(x)+ { + if (x>=1) 0 + else x^2 + }> myfunction(x)[1] 0.09 1.00 0.49 484.00 Warning message: the condition has length > 1 and only the first element will be used in: if (x >= 1) 0 else x^2>Is there a way to overcome this issue? Can you please explain what modifications to the code I need to accommodate to make it work. Thanks, Nawaf
nawaf b wrote:> Hi - > > I was wondering if anyone came across a problem such as mine! I was trying > to create a user-defined function, however, the results from calling the > function are unexpected! > > When passing X parameter as a single value variable (x<-c(3)), everything > works fine. However, when passing a parameter as a vector with multiple > values (as the case in my code), the 'if condition' is only executed once! > > Here is my code: > >>x > > [1] 0.3 1.0 0.7 22.0 > >>myfunction<-function(x) > > + { > + if (x>=1) 0 > + else x^2 > + }?ifelse myfunction <- function(x){ifelse(x >= 1, 0, x^2)} > myfunction(c(.3, 1, .7, 22)) [1] 0.09 0.00 0.49 0.00>>myfunction(x) > > [1] 0.09 1.00 0.49 484.00 > Warning message: > the condition has length > 1 and only the first element will be used in: if > (x >= 1) 0 else x^2 > > > Is there a way to overcome this issue? Can you please explain what > modifications to the code I need to accommodate to make it work. > > Thanks, > > Nawaf > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
ifelse(x >= 1, 0, x^2) or (x >= 1) * x^2 Also, you might need x > (1 - e) where e is some small number since your numbers may not be exact. On 10/8/05, nawaf b <talda at hotmail.com> wrote:> Hi - > > I was wondering if anyone came across a problem such as mine! I was trying > to create a user-defined function, however, the results from calling the > function are unexpected! > > When passing X parameter as a single value variable (x<-c(3)), everything > works fine. However, when passing a parameter as a vector with multiple > values (as the case in my code), the 'if condition' is only executed once! > > Here is my code: > > x > [1] 0.3 1.0 0.7 22.0 > > myfunction<-function(x) > + { > + if (x>=1) 0 > + else x^2 > + } > > myfunction(x) > [1] 0.09 1.00 0.49 484.00 > Warning message: > the condition has length > 1 and only the first element will be used in: if > (x >= 1) 0 else x^2 > > > > Is there a way to overcome this issue? Can you please explain what > modifications to the code I need to accommodate to make it work. > > Thanks, > > Nawaf > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Sat, 8 Oct 2005, nawaf b wrote:> Hi - > > I was wondering if anyone came across a problem such as mine! I was trying > to create a user-defined function, however, the results from calling the > function are unexpected! > > When passing X parameter as a single value variable (x<-c(3)), everything > works fine. However, when passing a parameter as a vector with multiple > values (as the case in my code), the 'if condition' is only executed once! > > Here is my code: > > x > [1] 0.3 1.0 0.7 22.0 > > myfunction<-function(x) > + { > + if (x>=1) 0 > + else x^2 > + } > > myfunction(x) > [1] 0.09 1.00 0.49 484.00 > Warning message: > the condition has length > 1 and only the first element will be used in: if > (x >= 1) 0 else x^2 > > > > Is there a way to overcome this issue? Can you please explain what > modifications to the code I need to accommodate to make it work.> myfunction<-function(x)+ ifelse(x>=1, 0, x^2)> x <- c(0.3, 1.0, 0.7, 22.0) > myfunction(x)[1] 0.09 0.00 0.49 0.00 ifelse() is vectorised, but if() only looks at the first element in x before deciding what to do - so if the condition is longer, the rest gets ignored.> > Thanks, > > Nawaf > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
nawaf b wrote:> Hi - > > I was wondering if anyone came across a problem such as mine! I was trying > to create a user-defined function, however, the results from calling the > function are unexpected! > > When passing X parameter as a single value variable (x<-c(3)), everything > works fine. However, when passing a parameter as a vector with multiple > values (as the case in my code), the 'if condition' is only executed once! > > Here is my code: > >>x > > [1] 0.3 1.0 0.7 22.0 > >>myfunction<-function(x) > > + { > + if (x>=1) 0 > + else x^2 > + } > >>myfunction(x) > > [1] 0.09 1.00 0.49 484.00 > Warning message: > the condition has length > 1 and only the first element will be used in: if > (x >= 1) 0 else x^2 > > > Is there a way to overcome this issue? Can you please explain what > modifications to the code I need to accommodate to make it work.I am going to go out on a limb here (I don't know much about R) but I'm not sure it has anything to do with that fact that you have defined a function. It seems to me that "if (x > 1) {...}" expects a scalar and if x is not a scalar, it will use the first value. To apply your function to each element, check out the help for sapply (eg ?sapply). I am guessing the solution is something like sapply(x, myfunction) but don't quote me. paul sorenson
Possibly Parallel Threads
- Adding a new conditional column to a list of dataframes
- R CMD check: Error in .C
- help - "the condition has length > 1 and only the first element will be used"
- Adding a new conditional column to a list of dataframes
- Referencing function name from within function