And, of course, there's transform, as David knows very well: d <- transform(d, c = (a + b) > 25) > head(d) a b z c 1 1 2001 5001 TRUE 2 2 2002 5002 TRUE 3 3 2003 5003 TRUE 4 4 2004 5004 TRUE 5 5 2005 5005 TRUE 6 6 2006 5006 TRUE
As far as David's 'wrong way' is concerned, I think he may have meant to use within() instead of with(): d0 <- d[, -4] > head(d0, 2) a b z 1 1 2001 5001 2 2 2002 5002 d2 <- within(d0, c <- (a + b) > 25) > identical(d, d2) [1] TRUE HTH, Dennis On Mon, Aug 23, 2010 at 5:36 PM, David Winsemius <dwinsem...@comcast.net>wrote: > > On Aug 23, 2010, at 6:28 PM, David Winsemius wrote: > > >> On Aug 23, 2010, at 5:51 PM, ivo welch wrote: >> >> quizz---what does this produce? >>> >>> d=data.frame( a=1:1000, b=2001:3000, z= 5001:6000 ) >>> attach(d); c <- (a+b)>25; detach(d) >>> d= subset(d, TRUE, select=c( a, b, c )) >>> >>> yes, I know I have made a mistake, in that the code does not do what I >>> presumably would have wanted. it does seem like unexpected behavior, >>> though, without an error. there probably is some reason why this does >>> not ring an alarm bell... >>> >> >> You have created a perfect example for why it is a bad idea to attach >> data.frames. >> >> ?attach # yes, I am yet again saying: "read the help page..." >> >> ... especially the 4th paragraph of the Details section. >> > > I think it might helpful to consider the right way and the wrong way to do > the same assignment using with(), which is my choice as an alternative to > attache > > Right; > > d$c <- with(d, a+b >25) # note: using "c" as an object name is a really > confusing strategy > > Wrong: > with(d, c <- a+b <25) > > The wrong way is similar to what you might have thought would be happening. > The attach() operation created its own environment, but that did not > necessarily mean that all assignments would be creating new columns inside > "d". > > > >> -- >> David. >> >> >> >> David Winsemius, MD >> West Hartford, CT >> >> ______________________________________________ >> R-help@r-project.org 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. >> > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > R-help@r-project.org 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org 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.