Re: [R] Return value from function with For loop

2017-04-17 Thread William Dunlap via R-help
A long time ago (before the mid-1990's?) with S or S+ ff <- function(n){ for(i in 1:n) (i+1) op <- ff(3) would result in 'op' being 4, since a for-loop's value was the value of the last expression executed in the body of the loop. The presence of a 'next' or 'break' in the loop body would

Re: [R] Return value from function with For loop

2017-04-17 Thread Boris Steipe
Ramnik, a final mail is actually really important: this is to document in the archives, for the benefit of those who found the thread at a later time, that the responses indeed solved the problem. Other than that, the single most important advice is to - provide a minimal working example of

Re: [R] Return value from function with For loop

2017-04-17 Thread Bert Gunter
(Apparently I hit "send" too early) 1. I have cc'ed this to the list, as others may well have some good suggestions re: books. 2. The posting guide is your best resource as to what is appropriate for the list. I defer to others re: conventions, as I have have been accused of violating them from

Re: [R] Return value from function with For loop

2017-04-17 Thread Bert Gunter
OK. I stand corrected. Thanks. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Apr 16, 2017 at 11:36 PM, David Winsemius

Re: [R] Return value from function with For loop

2017-04-17 Thread peter dalgaard
> On 17 Apr 2017, at 10:04 , Ramnik Bansal wrote: > > This is my output for is.function > >> is.function("for") > [1] FALSE >> is.function(for) > Error: unexpected ')' in "is.function(for)" >> is.function("next") > [1] FALSE >> is.function(next) > Error: no loop for

Re: [R] Return value from function with For loop

2017-04-17 Thread Ramnik Bansal
This is my output for is.function > is.function("for") [1] FALSE > is.function(for) Error: unexpected ')' in "is.function(for)" > is.function("next") [1] FALSE > is.function(next) Error: no loop for break/next, jumping to top level *I did not get the TRUE value. R version 3.3.3 on Mac. What am I

Re: [R] Return value from function with For loop

2017-04-17 Thread David Winsemius
Both 'for' and 'next' return TRUE from is.function is.function('for') is.function('next') Not at an R console at the moment but I did check this earlier today. Thinking of it as different is definitely the way to think about it. (ISTR Bert and I have had this exchange in the past.) -- Best

Re: [R] Return value from function with For loop

2017-04-16 Thread Bert Gunter
David et. al.: "this levels is the level where you realize that the `for` function is different from most other R functions. It is really a side-effect-fucntion. " for(), while(), if(), next, etc. are *not* functions. ?for says: "These are the basic control-flow constructs of the R language."

Re: [R] Return value from function with For loop

2017-04-16 Thread David Winsemius
> On Apr 16, 2017, at 7:26 PM, Ramnik Bansal wrote: > > In the code below > > > *ff <- function(n){ for(i in 1:n) (i+1)}* > > *n<-3;ff(n)->op;print(op)* > > Why doesnt *print(op) * print 4 and instead prints NULL. > Isnt the last line of code executed is *i+1 * and

Re: [R] Return value from function with For loop

2017-04-16 Thread jim holtman
In the first case you have a "for" and it is the statement after the 'for' that is the return value and it is a NULL. For example: > print(for (i in 1:4) i+1) NULL In the second case, the last statement if the expression '(n+1)' which give you the correct value: > xx <- function(n) n+1 >