[R] understanding the FUNCTION function
Sarah Goslee
sarah.goslee at gmail.com
Thu Apr 26 20:35:56 CEST 2012
In R, the preferred method is to assign the result to a new object:
fac <- function(x) {
a<-1
for(i in 1:x){
a<-a*i
print(a)
}
a # need to explicitly state what the function should return
}
myresult <- fac(5)
myresult
Sarah
On Thu, Apr 26, 2012 at 2:16 PM, michaelyb <cel81009759 at gmail.com> wrote:
> Ista,
>
> Since you seem to know your stuff very well, how would you get 120 out of a
> function that gives you the factorial of 5, without using factorial(5)?
>
>> Meanwhile, look at this example instead:
>> fac<-function(x){a<-1
>> for(i in 1:x){
>> a<-a*i
>> print(a)}}
>
> gives you 120, but you cannot access it after the end of execution.
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list