increase or decrease variable by 1

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

increase or decrease variable by 1

josh2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Uwe Ligges-3


On 07.12.2010 14:43, madr wrote:
>
> many languages have shorthands for that operation like:
>
> variable += 1
> or
> ++variable
>
> is there something like that in R ?

No.

Uwe Ligges

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Gabor Grothendieck
In reply to this post by josh2
On Tue, Dec 7, 2010 at 8:43 AM, madr <[hidden email]> wrote:
>
> many languages have shorthands for that operation like:
>
> variable += 1
> or
> ++variable
>
> is there something like that in R ?


You can do this:

> x <- 3
> `+`(x) <- 1
> x
[1] 4



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Ivan Calandra
x+1 is not that complicated... Am I missing something here?

Le 12/7/2010 16:55, Gabor Grothendieck a écrit :

> On Tue, Dec 7, 2010 at 8:43 AM, madr<[hidden email]>  wrote:
>> many languages have shorthands for that operation like:
>>
>> variable += 1
>> or
>> ++variable
>>
>> is there something like that in R ?
>
> You can do this:
>
>> x<- 3
>> `+`(x)<- 1
>> x
> [1] 4
>
>
>

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Ted Harding
Indeed!

  x <- x + 1

(and being generous with unnecessary spaces) uses 10 characters.

  `+`(x)<-1

(being mean with them) uses 9. The "mean" version of the first
uses only 6: x<-x+1

However, I suppose there is merit in the spiritual exercise
of contemplating how `+`(x)<-1 gets worked out!

Ted.

On 07-Dec-10 16:23:17, Ivan Calandra wrote:

> x+1 is not that complicated... Am I missing something here?
>
> Le 12/7/2010 16:55, Gabor Grothendieck a écrit :
>> On Tue, Dec 7, 2010 at 8:43 AM, madr<[hidden email]>  wrote:
>>> many languages have shorthands for that operation like:
>>>
>>> variable += 1
>>> or
>>> ++variable
>>>
>>> is there something like that in R ?
>>
>> You can do this:
>>
>>> x<- 3
>>> `+`(x)<- 1
>>> x
>> [1] 4
> --
> Ivan CALANDRA

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Dec-10                                       Time: 16:42:44
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Bert Gunter
Ted:

Inline below...

On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding <[hidden email]> wrote:

> Indeed!
>
>  x <- x + 1
>
> (and being generous with unnecessary spaces) uses 10 characters.
>
>  `+`(x)<-1
>
> (being mean with them) uses 9. The "mean" version of the first
> uses only 6: x<-x+1
>
> However, I suppose there is merit in the spiritual exercise
> of contemplating how `+`(x)<-1 gets worked out!

AFAICS it doesn't.
> `+`(x)<-1
Error in +x <- 1 : could not find function "+<-"

`+`(x,1) does:

> `+`(x,1)
[1] 4

-- Bert



>
> Ted.
>
> On 07-Dec-10 16:23:17, Ivan Calandra wrote:
>> x+1 is not that complicated... Am I missing something here?
>>
>> Le 12/7/2010 16:55, Gabor Grothendieck a écrit :
>>> On Tue, Dec 7, 2010 at 8:43 AM, madr<[hidden email]>  wrote:
>>>> many languages have shorthands for that operation like:
>>>>
>>>> variable += 1
>>>> or
>>>> ++variable
>>>>
>>>> is there something like that in R ?
>>>
>>> You can do this:
>>>
>>>> x<- 3
>>>> `+`(x)<- 1
>>>> x
>>> [1] 4
>> --
>> Ivan CALANDRA
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <[hidden email]>
> Fax-to-email: +44 (0)870 094 0861
> Date: 07-Dec-10                                       Time: 16:42:44
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> [hidden email] 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.
>



--
Bert Gunter
Genentech Nonclinical Biostatistics

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Gabor Grothendieck
On Tue, Dec 7, 2010 at 12:25 PM, Bert Gunter <[hidden email]> wrote:

> Ted:
>
> Inline below...
>
> On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding <[hidden email]> wrote:
>> Indeed!
>>
>>  x <- x + 1
>>
>> (and being generous with unnecessary spaces) uses 10 characters.
>>
>>  `+`(x)<-1
>>
>> (being mean with them) uses 9. The "mean" version of the first
>> uses only 6: x<-x+1
>>
>> However, I suppose there is merit in the spiritual exercise
>> of contemplating how `+`(x)<-1 gets worked out!
>
> AFAICS it doesn't.
>> `+`(x)<-1
> Error in +x <- 1 : could not find function "+<-"

Sorry, my code was missing the first line:

> `+<-` <- `+`
>
> x <- 3
> `+`(x) <- 1
> x
[1] 4



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

David Winsemius

On Dec 7, 2010, at 12:42 PM, Gabor Grothendieck wrote:

> On Tue, Dec 7, 2010 at 12:25 PM, Bert Gunter  
> <[hidden email]> wrote:
>> Ted:
>>
>> Inline below...
>>
>> On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding  
>> <[hidden email]> wrote:
>>> Indeed!
>>>
>>>  x <- x + 1
>>>
>>> (and being generous with unnecessary spaces) uses 10 characters.
>>>
>>>  `+`(x)<-1
>>>
>>> (being mean with them) uses 9. The "mean" version of the first
>>> uses only 6: x<-x+1
>>>
>>> However, I suppose there is merit in the spiritual exercise
>>> of contemplating how `+`(x)<-1 gets worked out!
>>
>> AFAICS it doesn't.
>>> `+`(x)<-1
>> Error in +x <- 1 : could not find function "+<-"
>
> Sorry, my code was missing the first line:
>
>> `+<-` <- `+`
>>
>> x <- 3
>> `+`(x) <- 1
>> x
> [1] 4

One can also attempt further creative violence to the language that  
shows that is possible to construct unary operators like the C  
prefix-"+" that do not require shift-9 <operand> shift-0.

 > `+`
function (e1, e2)  .Primitive("+")
 > `+` <- `-`    # in a different universe perhaps
 >  4 + 2
[1] 2
 > `+` <- function (e1, e2)  .Primitive("+") # attempt to restore
 >  4 + 2    # test
function (e1, e2)  .Primitive("+")  ##  oops
 > `+` <-  .Primitive("+")
 >  4 + 2
[1] 6          # "works"
 > `!` <- function(x) x + 1   # hijacking a logical NOT definition
 > !3
[1] 4   # so this is a redefinition of one of the two unary operators
         # ( of which I am aware, I'm not an R-guru.)

 > `!` <- .Primitive("!") ## hoping to restore expected behavior
 > !3
[1] FALSE   # whew. That seemed dangerous but I appear to have survived.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Gabor Grothendieck
On Tue, Dec 7, 2010 at 1:12 PM, David Winsemius <[hidden email]> wrote:

>
> On Dec 7, 2010, at 12:42 PM, Gabor Grothendieck wrote:
>
>> On Tue, Dec 7, 2010 at 12:25 PM, Bert Gunter <[hidden email]>
>> wrote:
>>>
>>> Ted:
>>>
>>> Inline below...
>>>
>>> On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding <[hidden email]>
>>> wrote:
>>>>
>>>> Indeed!
>>>>
>>>>  x <- x + 1
>>>>
>>>> (and being generous with unnecessary spaces) uses 10 characters.
>>>>
>>>>  `+`(x)<-1
>>>>
>>>> (being mean with them) uses 9. The "mean" version of the first
>>>> uses only 6: x<-x+1
>>>>
>>>> However, I suppose there is merit in the spiritual exercise
>>>> of contemplating how `+`(x)<-1 gets worked out!
>>>
>>> AFAICS it doesn't.
>>>>
>>>> `+`(x)<-1
>>>
>>> Error in +x <- 1 : could not find function "+<-"
>>
>> Sorry, my code was missing the first line:
>>
>>> `+<-` <- `+`
>>>
>>> x <- 3
>>> `+`(x) <- 1
>>> x
>>
>> [1] 4
>
> One can also attempt further creative violence to the language that shows
> that is possible to construct unary operators like the C prefix-"+" that do
> not require shift-9 <operand> shift-0.
>
>> `+`
> function (e1, e2)  .Primitive("+")
>> `+` <- `-`    # in a different universe perhaps
>>  4 + 2
> [1] 2
>> `+` <- function (e1, e2)  .Primitive("+") # attempt to restore
>>  4 + 2    # test
> function (e1, e2)  .Primitive("+")  ##  oops
>> `+` <-  .Primitive("+")
>>  4 + 2
> [1] 6          # "works"
>> `!` <- function(x) x + 1   # hijacking a logical NOT definition
>> !3
> [1] 4   # so this is a redefinition of one of the two unary operators
>        # ( of which I am aware, I'm not an R-guru.)
>
>> `!` <- .Primitive("!") ## hoping to restore expected behavior
>> !3
> [1] FALSE   # whew. That seemed dangerous but I appear to have survived.

Unlike the above, +<- is not predefined so its definition does not
overwrite existing definitions.

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: increase or decrease variable by 1

Duncan Murdoch-2
In reply to this post by Gabor Grothendieck
On 07/12/2010 12:42 PM, Gabor Grothendieck wrote:

> On Tue, Dec 7, 2010 at 12:25 PM, Bert Gunter<[hidden email]>  wrote:
>> Ted:
>>
>> Inline below...
>>
>> On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding<[hidden email]>  wrote:
>>> Indeed!
>>>
>>>   x<- x + 1
>>>
>>> (and being generous with unnecessary spaces) uses 10 characters.
>>>
>>>   `+`(x)<-1
>>>
>>> (being mean with them) uses 9. The "mean" version of the first
>>> uses only 6: x<-x+1
>>>
>>> However, I suppose there is merit in the spiritual exercise
>>> of contemplating how `+`(x)<-1 gets worked out!
>>
>> AFAICS it doesn't.
>>> `+`(x)<-1
>> Error in +x<- 1 : could not find function "+<-"
>
> Sorry, my code was missing the first line:
>
>> `+<-`<- `+`
>>
>> x<- 3
>> `+`(x)<- 1
>> x
> [1] 4

Note that there are at least two subtle differences between your code
and x += 1 or ++x:

First, the value of `+`(x)<- 1 is 1, i.e.

  print(`+`(x)<- 1)

will give 1 regardless of x, unlike ++x.

Another difference is that  `+`(x)<- 1 is equivalent to x <- x + 1, and
in R, that doesn't necessarily increment x:  the x on the right hand
side might be a global variable, and the result of x + 1 will be
assigned to a new local variable.  For example, the sequence

x <- 3
f <- function() `+`(x)<- 1
f()
x

leaves x set to 3.  (But it temporarily created a new variable called x
in the evaluation frame of f().)

I don't think the first difference is documented; I haven't checked the
source to know if it's intentional.  (It generally makes sense that "foo
<- bar" has value bar; the problem is that your "assignment" doesn't
follow the rule that it assigns bar to foo.)

The second one is a basic fact of life in R, and the source of a few
bugs:  the only way around it that I can see would be to allow users to
declare things about variables (e.g. "x is a global variable, don't
create a local when I assign to it!").

Duncan Murdoch

______________________________________________
[hidden email] 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.