[R] Trying to learn how to write a function

2023-03-16 Thread Sorkin, John
I am trying to understand how to write an "advanced" function. To do this, I am examining the code of lm, a small part of the lm code is below. N > lm function (formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, c

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread William Dunlap
altered. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of David Winsemius > Sent: Friday, September 07, 2012 1:35 PM > To: wwreith > Cc: r-help@r-

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Ista Zahn
On Fri, Sep 7, 2012 at 3:16 PM, Rui Barradas wrote: > Hello, > > Start by (re-)reading An Introduction to R, file R-intro.pdf in your doc > directory. Chapter 10. > > You're just computing, not returning the result of that computation. Nor > anything else, ADD() does not return a value. Sure it d

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread David Winsemius
On Sep 7, 2012, at 1:34 PM, David Winsemius wrote: > > On Sep 7, 2012, at 11:00 AM, wwreith wrote: > >> I am just starting to experiment with writing a function and have run into >> what seems like a limitation or more likely a lack of understanding on my >> part. >> >> Very Simple Example:

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread David Winsemius
On Sep 7, 2012, at 11:00 AM, wwreith wrote: > I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-fu

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Rui Barradas
Hello, Start by (re-)reading An Introduction to R, file R-intro.pdf in your doc directory. Chapter 10. You're just computing, not returning the result of that computation. Nor anything else, ADD() does not return a value. (See the example in R-intro, section 10.1) And you need to assign the

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Bert Gunter
WHEW! Have you read An Introduction to R? If not, stop and do so. If you have, stop and do so again. Or look for other web tutorials (you may get some suggestions here) or books (CRAN is a good place to look for this). The problem is that you are so far from understanding how a function works, tha

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Berend Hasselman
On 07-09-2012, at 21:54, William Dunlap wrote: > The functions > f1 <- function(x) x <- x + 1 > and > f2 <- function(x) x + 1 > both return the same value. You can see > this by doing > z1 <- f1(10) ; print(z1) > z2 <- f2(10) ; print(z2) > or just > print(f1(10)) > print(f2(10)) > >

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread William Dunlap
gt; To: William Dunlap > Cc: wwreith; r-help@r-project.org > Subject: Re: [R] Trying to learn how to write a function... can't define a > variable?? > > > On 07-09-2012, at 21:32, William Dunlap wrote: > > > Berend, > > The OP's ADD() is returning the new v

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Berend Hasselman
On 07-09-2012, at 21:32, William Dunlap wrote: > Berend, > The OP's ADD() is returning the new value of x, as do all > of your alternatives. His problem is that he is not assigning > the output of ADD to a variable, as in > z <- ADD(z) > Ordinary functions do not alter their arguments. The

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread William Dunlap
- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Berend Hasselman > Sent: Friday, September 07, 2012 12:18 PM > To: wwreith > Cc: r-help@r-project.org > Subject: Re: [R] Trying to learn how to write a function... can't define

Re: [R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread Berend Hasselman
On 07-09-2012, at 20:00, wwreith wrote: > I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-functi

[R] Trying to learn how to write a function... can't define a variable??

2012-09-07 Thread wwreith
I am just starting to experiment with writing a function and have run into what seems like a limitation or more likely a lack of understanding on my part. Very Simple Example: I want to define a function that does 1+1=2. z<-1 ADD<-function(x) { x<-x+1 } ADD(z) z output for z is 1 not the expec