[R] increase or decrease variable by 1

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 7 19:19:26 CET 2010


On Tue, Dec 7, 2010 at 1:12 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Dec 7, 2010, at 12:42 PM, Gabor Grothendieck wrote:
>
>> On Tue, Dec 7, 2010 at 12:25 PM, Bert Gunter <gunter.berton at gene.com>
>> wrote:
>>>
>>> Ted:
>>>
>>> Inline below...
>>>
>>> On Tue, Dec 7, 2010 at 8:42 AM, Ted Harding <ted.harding at wlandres.net>
>>> 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



More information about the R-help mailing list