[R] abbreviate

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jun 16 01:31:47 CEST 2005


On 6/15/05, Marc Schwartz <MSchwartz at mn.rr.com> wrote:
> On Wed, 2005-06-15 at 19:33 +0100, Prof Brian Ripley wrote:
> > On Wed, 15 Jun 2005, Omar Lakkis wrote:
> >
> > >> p = data.frame(high=c(5,2), settle=c(3,4))
> > >> p
> > >  high settle
> > > 1    5      3
> > > 2    2      4
> > >
> > > What is the most abbreviated way to apply:
> > > if (p$high < p$settle) p$high = p$settle
> > >
> > > I want to modify p to become:
> > >> p
> > >  high settle
> > > 1    5      3
> > > 2    4      4
> >
> > p[[1]] <- pmax(p[[1]], p[[2]])
> >
> > seems to need a rather small number of keystrokes at the expense of
> > readability (I would otherwise use p$high etc).
> 
> I do like that approach. Definitely less keystrokes...
> 
> :-)
> 

Agree that this definitely should be pursued. :)  In fact,
we can shave off several keystrokes by 

- replacing p[[1]] with p[1] on the left hand side
- p$high and p$settle with p$h and p$s on the right hand
  side (which makes use of the matching property of $)
- '=' instead of '<-'
- remove all the spaces

This gets it down to 18 characters:

p[1]=pmax(p$h,p$s)




More information about the R-help mailing list