if just integer exponentiation is enough, then I'd suggest: expf <- function(x, n) { return(prod(rep(x, abs(n)))^sign(n)) } cheers. /federico On Wed, Jul 13, 2011 at 6:24 AM, Ben Bolker wrote: > mousy0815 gmail.com> writes: > > > I'm trying to make a function that will output the exponent... so f2(2,2) > = 4 > > and f2(2,3)=8. But I don't want to just use the x^n function, I want to > do > > it another way, and without a recursion. I did the follow code but for > some > > reason it doesn't work. Help please? > > > > f2 <- function(x, n) #without recursion { > > y <- 1 > > if (n==0) {return(1)} else { > > if (length(y) < (n+1) { > > y <-append(y, x, after = length(y)) > > } else {return(prod(y))} > > > > } > > } > > > > Since this list is not intended for homework (see the posting > guide) can you please tell us why (other than homework) you might > be trying to do this? > > Ben Bolker > > ______________________________________________ > 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. > [[alternative HTML version deleted]]