All,
My goofball mistake yesterday still elicited some good responses,
suggestions, and explanations. Here is the original message:
> > All,
> >
> > S-Plus 2000 on Windows 2000
> >
> > I am trying to create some binary data from columns in a data frame.
> > When I issue the command
> >
> > > TTS.Predictors.Cats <- ifelse(Cool.TTS$Deck1.Hgt > 0 &
> > Cool.TTS$Deck1.Hgt < 3048, 1, 0)
> >
> > I think I am going to get 1's when the value is within the
> > range and 0's
> > when it is not. Instead, I get all NA's. If I use one
> > condition or the
> > other, it works properly, but I need to have 1's when both
conditions
> > are true, not just one. What am I doing wrong? Thanks for the
help.
> >
> > Win
Shawn Boles suggested I try &&, and when I went into my script to try it
I noticed that I had ised $ instead of &. I modified the script using &
and it worked. As a test, I tried && and it didn't work - the command
only accessed the first element in the column and not the rest. Dave
Nelson explained why:
> > Yes, && will most definitely NOT work: ifelse wants the conditional
> > expression to return a vector of booleans, which & happily provides,
while
> > && wants its arguments to be single booleans and returns a single
boolean.
> > This is a common error that new C programmers make as well: & is a
> > bit-twiddling operation, while && is a logical operation.
Rolf Turner sent an email to the entire newsgroup with the subject &&,
so I'll only repeat his first paragraph:
> > This is a trap that I fall into repeatedly --- the double logical
> > connectives (&& and ||) work only with scalar logicals. (Why can't
I
> > remember that?) The implication of using the double symbol rather
> > than the single is that the second argument is evaluated ***only***
> > if it is really needed. For && the second argument is evaluated
only
> > if the first argument is true.
For the rest of what he wrote, see his email from 26 Sep. Finally, Jeff
Laake suggested a much cleaner way of creating my binary predictors.
Since the output from my routines will only be 0 or 1, it would be much
more direct to use
> TTS.Predictors.Cats <- as.numeric(Cool.TTS$Deck1.Hgt > 0 &
Cool.TTS$Deck1.Hgt < 3048)
If you want something other than 0s or 1s, then ifelse is the way to go.
Thanks all!
W
******************************************************
Winifred C. Lambert, Senior Scientist/Meteorologist
ENSCO Inc.
Aerospace Sciences and Engineering Division
1980 N Atlantic Ave, Suite 230
Cocoa Beach, FL 32931
VOICE: 321.853.8130 FAX: 321.853.8415
lambert.winifred@ensco.com
AMU Quarterly Reports on the Web:
http://technology.ksc.nasa.gov/WWWaccess/AMU/home.html
******************************************************
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|