[R] Multiple Conditional Tranformations

Gabor Grothendieck ggrothendieck at gmail.com
Sat Nov 25 06:38:56 CET 2006


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.
> >
>



More information about the R-help mailing list