> p = data.frame(high=c(5,2), settle=c(3,4)) > phigh 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:> phigh settle 1 5 3 2 4 4
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 4p[[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). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Wed, 2005-06-15 at 13:59 -0400, 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 4Probably the easiest would be:> phigh settle 1 5 3 2 2 4> p$high <- with(p, ifelse(high < settle, settle, high))> phigh settle 1 5 3 2 4 4 See ?ifelse and ?with HTH, Marc Schwartz
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... :-) Marc