[R] Multiple Conditional Tranformations
Muenchen, Robert A (Bob)
muenchen at utk.edu
Sat Nov 25 20:45:37 CET 2006
I have a program that is similar to your longer version, but I could
never get the syntax quite right. This will be a big help in
understanding how by works with functions.
Thanks,
Bob
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Saturday, November 25, 2006 11:11 AM
To: Muenchen, Robert A (Bob)
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Multiple Conditional Tranformations
Here is a correction:
do.call(rbind, by(mydata, 1:nrow(mydata), function(x)
switch(as.character(x$gender),
m = transform(x, score1 = 3*q1+q2, score2 = 3.5*q1+q2),
f = transform(x, score1 = 2*q1+q2, score2 = 2.5*q1+q2),
transform(x, score1 = NA, score2 = NA))
))
On 11/25/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Here are some additional solutions. It appears that the SAS code is
performing
> the transformation row by row and for each row the code in your post
is
> specifying the transformation so if you want to do it that way we
> could use 'by'
> like this (where this time we have also added NA processing for the
gender):
>
>
> do.call(rbind, by(mydata, 1:nrow(mydata), function(x)
> switch(as.character(x$gender),
> m = transform(x, score1 = 3*q1+q2, score2 = 3.5*q1+q2),
> f = transform(x, score1 = 2*q1+q2, score2 = 2.5*q1+q2),
> NA)
> ))
>
> # or this somewhat longer version:
>
> do.call(rbind, by(mydata, 1:nrow(mydata), function(x) with(x, {
> if (is.na(gender)) {
> score1 <- score2 <- NA
> } else if (gender == "m") {
> score1 = 3 * q1 + q2
> score2 = 3.5 * q1 + q2
> } else if (gender == "f") {
> score1 = 2 * q1 + q2
> score2 = 2.5 * q1 + q2
> }
> cbind(x, score1, score2)
> })))
>
>
>
>
>
>
>
> On 11/25/06, Muehnchen, Robert A (Bob) <muenchen at utk.edu> wrote:
> > That's exactly what I'm looking for. Thanks so much for taking the
time
> > to do it that way.
> >
> > On the redundancy issue, I think SAS checks the "else if" condition
only
> > if the original "if" is false. The check for f when not m I put in
only
> > to exclude missing values for gender.
> >
> > Thanks!!
> > Bob
> >
> > -----Original Message-----
> > From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
> > Sent: Saturday, November 25, 2006 7:37 AM
> > To: Muenchen, Robert A (Bob)
> > Cc: r-help at stat.math.ethz.ch
> > Subject: Re: [R] Multiple Conditional Tranformations
> >
> > Firstly your outline does not check once, it checks twice. First it
> > check for "m" and then it redundantly checks for "f". On the other
> > hand the two variations in my post do check once.
> >
> > Although substantially longer than the solutions in my prior posts,
> > if you want the style shown in your post try this:
> >
> > mydata2 <- cbind(mydata, score1 = 0, score2 = 0)
> > is.m <- mydata$gender == "m"
> >
> > mydata2[is.m, ] <- transform(mydata[is.m, ],
> > score1 = 3 * q1 + q2,
> > score2 = 3.5 * q1 + q2
> > )
> >
> > mydata2[!is.m,] <- transform(mydata2[!is.m, ],
> > score1 = 2 * q1 + q2,
> > score2 = 2.5 * q1 + q2
> > )
> >
> > On 11/25/06, Muenchen, Robert A (Bob) <muenchen at utk.edu> wrote:
> > > Gabor,
> > >
> > > Those are handy variations! Perhaps my brain in still in SAS mode
on
> > > this. I'm expecting something like the code below that checks for
male
> > > only once, checks for female only when not male (skipping NAs) and
> > does
> > > all formulas under the appropriate conditions. The formulas I made
up
> > to
> > > keep the code short & may not be as easily modified to let the
logical
> > > 0/1 values fix them.
> > >
> > > if gender=="m" then do;
> > > Score1=...
> > > Score2=
> > > ...
> > > end;
> > > else if gender=="f" then do;
> > > Score1=...
> > > Score2=
> > > ...
> > > end;
> > >
> > > R may not have anything quite like that. R certainly has many
other
> > > features that SAS lacks.
> > >
> > > Thanks,
> > > Bob
> > >
> > > =========================================================
> > > Bob Muenchen (pronounced Min'-chen), Manager
> > > Statistical Consulting Center
> > > U of TN Office of Information Technology
> > > 200 Stokely Management Center, Knoxville, TN 37996-0520
> > > Voice: (865) 974-5230
> > > FAX: (865) 974-4810
> > > Email: muenchen at utk.edu
> > > Web: http://oit.utk.edu/scc,
> > > News: http://listserv.utk.edu/archives/statnews.html
> > > =========================================================
> > >
> > >
> > > -----Original Message-----
> > > From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
> > > Sent: Saturday, November 25, 2006 12:39 AM
> > > To: Muenchen, Robert A (Bob)
> > > Cc: r-help at stat.math.ethz.ch
> > > Subject: Re: [R] Multiple Conditional Tranformations
> > >
> > > And here is a variation:
> > >
> > > transform(mydata,
> > > score1 = (2 + (gender == "m")) * q1 + q2,
> > > score2 = score1 + 0.5 * q1
> > > )
> > >
> > > or
> > >
> > > transform(
> > > transform(mydata, score1 = (2 + (gender == "m")) * q1 + q2),
> > > score2 = score1 + 0.5 * q1
> > > )
> > >
> > >
> > > On 11/25/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > > > Try this:
> > > >
> > > >
> > > > transform(mydata,
> > > > score1 = (2 + (gender == "m")) * q1 + q2,
> > > > score2 = (2.5 + (gender == "m")) * q1 + q2
> > > > )
> > > >
> > > >
> > > > On 11/24/06, Muenchen, Robert A (Bob) <muenchen at utk.edu> wrote:
> > > > > Mark,
> > > > >
> > > > > I finally got that approach to work by spreading the logical
> > > condition
> > > > > everywhere. That gets the lengths to match. Still, I can't
help
> > but
> > > > > think there must be a way to specify the logic once per
condition.
> > > > >
> > > > > Thanks,
> > > > > Bob
> > > > >
> > > > > mydata$score1<-numeric(mydata$q1) #just initializing.
> > > > > mydata$score2<-numeric(mydata$q1)
> > > > > mydata$score1<-NA
> > > > > mydata$score2<-NA
> > > > > mydata
> > > > >
> > > > > mydata$score1[mydata$gender == "f"]<-
> > > 2*mydata$q1[mydata$gender=="f"] +
> > > > >
> > > > > mydata$q2[mydata$gender=="f"]
> > > > > mydata$score2[mydata$gender ==
> > > "f"]<-2.5*mydata$q1[mydata$gender=="f"] +
> > > > >
> > > > > mydata$q2[mydata$gender=="f"]
> > > > > mydata$score1[mydata$gender ==
> > "m"]<-3*mydata$q1[mydata$gender=="m"]
> > > +
> > > > > mydata$q2[mydata$gender=="m"]
> > > > > mydata$score2[mydata$gender ==
> > > "m"]<-3.5*mydata$q1[mydata$gender=="m"] +
> > > > >
> > > > > mydata$q2[mydata$gender=="m"]
> > > > > mydata
> > > > >
> > > > > =========================================================
> > > > > Bob Muenchen (pronounced Min'-chen), Manager
> > > > > Statistical Consulting Center
> > > > > U of TN Office of Information Technology
> > > > > 200 Stokely Management Center, Knoxville, TN 37996-0520
> > > > > Voice: (865) 974-5230
> > > > > FAX: (865) 974-4810
> > > > > Email: muenchen at utk.edu
> > > > > Web: http://oit.utk.edu/scc,
> > > > > News: http://listserv.utk.edu/archives/statnews.html
> > > > > =========================================================
> > > > >
> > > > >
> > > > > -----Original Message-----
> > > > > From: Leeds, Mark (IED) [mailto:Mark.Leeds at morganstanley.com]
> > > > > Sent: Friday, November 24, 2006 8:45 PM
> > > > > To: Muenchen, Robert A (Bob)
> > > > > Subject: RE: [R] Multiple Conditional Tranformations
> > > > >
> > > > > I'm not sure if I understand your question but I don't think
you
> > > need
> > > > > iflelse statements.
> > > > >
> > > > > myscore<-numeric(q1) ( because I'm not sure how to initialize
a
> > list
> > > so
> > > > > initialize a vector with q1 elements )
> > > > >
> > > > > myscore<-NA ( I think this should set all the values in
myscore to
> > > NA )
> > > > > myscore[mydata$gender == f]<-2*mydata$q1 + mydata$q2
> > > > > myscore[mydata$gender == m]<-3*mydata$q1 + mydata$q2
> > > > >
> > > > > the above should do what you do in the first part of your code
but
> > I
> > > > > don't know if that was your question ?
> > > > > also, it does it making myscore a vector because I didn't know
how
> > > to
> > > > > initialize a list.
> > > > > Someone else may goive a better solution. I'm no expert.
> > > > >
> > > > >
> > > > > -----Original Message-----
> > > > > From: r-help-bounces at stat.math.ethz.ch
> > > > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
Muenchen,
> > > Robert
> > > > > A (Bob)
> > > > > Sent: Friday, November 24, 2006 8:27 PM
> > > > > To: r-help at stat.math.ethz.ch
> > > > > Subject: [R] Multiple Conditional Tranformations
> > > > >
> > > > > Greetings,
> > > > >
> > > > >
> > > > >
> > > > > I'm learning R and I'm stuck on a basic concept: how to
specify a
> > > > > logical condition once and then perform multiple
transformations
> > > under
> > > > > that condition. The program below is simplified to demonstrate
the
> > > goal.
> > > > > Its results are exactly what I want, but I would like to check
the
> > > > > logical state of gender only once and create both (or any
number
> > of)
> > > > > scores at once.
> > > > >
> > > > >
> > > > >
> > > > > mystring<-
> > > > >
> > > > > ("id,group,gender,q1,q2,q3,q4
> > > > >
> > > > > 01,1,f,2,2,5,4
> > > > >
> > > > > 02,2,f,2,1,4,5
> > > > >
> > > > > 03,1,f,2,2,4,4
> > > > >
> > > > > 04,2,f,1,1,5,5
> > > > >
> > > > > 05,1,m,4,5,4,
> > > > >
> > > > > 06,2,m,5,4,5,5
> > > > >
> > > > > 07,1,m,3,3,4,5
> > > > >
> > > > > 08,2,m,5,5,5,4")
> > > > >
> > > > >
> > > > >
> > > > >
> > >
> >
mydata<-read.table(textConnection(mystring),header=TRUE,sep=",",row.name
> > > > > s="id")
> > > > >
> > > > > mydata
> > > > >
> > > > >
> > > > >
> > > > > #Create score1 so that it differs for males and females:
> > > > >
> > > > > mydata$score1 <- ifelse( mydata$gender=="f" ,
> > > > >
> > > > > (mydata$score1 <- (2*mydata$q1)+mydata$q2),
> > > > >
> > > > > ifelse( mydata$gender=="m",
> > > > >
> > > > > (mydata$score1 <- (3*mydata$q1)+mydata$q2), NA )
> > > > >
> > > > > )
> > > > >
> > > > > mydata
> > > > >
> > > > >
> > > > >
> > > > > #Create score2 so that it too differs for males and females:
> > > > >
> > > > > mydata$score2 <- ifelse( mydata$gender=="f" ,
> > > > >
> > > > > (mydata$score2 <- (2.5*mydata$q1)+mydata$q2),
> > > > >
> > > > > ifelse( mydata$gender=="m",
> > > > >
> > > > > (mydata$score2 <- (3.5*mydata$q1)+mydata$q2), NA )
> > > > >
> > > > > )
> > > > >
> > > > > mydata
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > Thanks!
> > > > >
> > > > > Bob
> > > > >
> > > > > =========================================================
> > > > > Bob Muenchen (pronounced Min'-chen), Manager Statistical
> > Consulting
> > > > > Center U of TN Office of Information Technology 200 Stokely
> > > Management
> > > > > Center, Knoxville, TN 37996-0520
> > > > > Voice: (865) 974-5230
> > > > > FAX: (865) 974-4810
> > > > > Email: muenchen at utk.edu
> > > > > Web: http://oit.utk.edu/scc <http://oit.utk.edu/scc> ,
> > > > > News: http://listserv.utk.edu/archives/statnews.html
> > > > > <http://listserv.utk.edu/archives/statnews.html>
> > > > > =========================================================
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > R-help at stat.math.ethz.ch 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.
> > > > > --------------------------------------------------------
> > > > >
> > > > > This is not an offer (or solicitation of an offer) to buy/sell
the
> > > > > securities/instruments mentioned or an official confirmation.
> > > Morgan
> > > > > Stanley may deal as principal in or own or act as market maker
for
> > > > > securities/instruments mentioned or may advise the issuers.
This
> > is
> > > not
> > > > > research and is not from MS Research but it may refer to a
> > research
> > > > > analyst/research report. Unless indicated, these views are
the
> > > author's
> > > > > and may differ from those of Morgan Stanley research or others
in
> > > the
> > > > > Firm. We do not represent this is accurate or complete and we
may
> > > not
> > > > > update this. Past performance is not indicative of future
> > returns.
> > > For
> > > > > additional information, research reports and important
> > disclosures,
> > > > > contact me or see https://secure.ms.com/servlet/cls. You
should
> > not
> > > use
> > > > > e-mail to request, authorize or effect the purchase or sale of
any
> > > > > security or instrument, to send transfer instructions, or to
> > effect
> > > any
> > > > > other transactions. We cannot guarantee that any such
requests
> > > received
> > > > > via e-mail will be processed in a timely manner. This
> > communication
> > > is
> > > > > solely for the addressee(s) and may contain confidential
> > > information.
> > > > > We do not waive confidentiality by mistransmission. Contact
me if
> > > you
> > > > > do not wish to receive these communications. In the UK, this
> > > > > communication is directed in the UK to those persons who are
> > market
> > > > > counterparties or intermediate customers (as defined in the UK
> > > Financial
> > > > > Services Authority's rules).
> > > > >
> > > > > ______________________________________________
> > > > > R-help at stat.math.ethz.ch 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.
> > > > >
> > > >
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch 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.
> > >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
>
More information about the R-help
mailing list