On 17-01-2012, at 13:36, Julia Burggraaf wrote: > Hello all, > > It might be a simple question, but I cannot find the solution, as I do not > know which subjects I should search on. So, much thanks for he/she we can > help me. > I am creating a function and would like to place a formula in the function, > without it being executed immediately. Like saving it temporary as 'text'. > > Simplified version of what I would like to be able to do: > > test<-function(a,x){ > if(a<5){ b<-3+ x[i]} > if(a>5){ b<- 6 + x[i]} > y<-1:10 > for (i in 1:10){y[i]<-4 + b} > return(y) > } > > In my perfect world, R will replace b in the formula y=4+b by the > appropriate b, indicated by the condition (value of a). > It now takes for 'b' only the first argument of x (+3 or 6). I know I can > solve the problem by also looping over b and turning it into a vector, but > I would like to know if it is also possible in the way stated above. If I > put 3+x[i] in "" to make it a character, it will still be character at > y<-4+b or when I use as.numeric, it will create NA....
You are complicating matters. test <- function(a,x){ if(a<5){ b <- 3+ x} else if(a>5){ b<- 6 + x} # b is now a vector y <- 4 + b return(y) } Puzzle for you to solve: what happens when a is identical to 5? Berend ______________________________________________ R-help@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.