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@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]]
Mark, Here's what I get when I try that approach. Thanks, Bob> mydata$score1<-numeric(mydata$q1) #just initializing. > mydata$score2<-numeric(mydata$q1) > mydata$score1<-NA > mydata$score2<-NA > mydatagroup gender q1 q2 q3 q4 score1 score2 1 1 f 2 2 5 4 NA NA 2 2 f 2 1 4 5 NA NA 3 1 f 2 2 4 4 NA NA 4 2 f 1 1 5 5 NA NA 5 1 m 4 5 4 NA NA NA 6 2 m 5 4 5 5 NA NA 7 1 m 3 3 4 5 NA NA 8 2 m 5 5 5 4 NA NA> mydata$score1[mydata$gender == "f"]<-2*mydata$q1 + mydata$q2Warning message: number of items to replace is not a multiple of replacement length> mydata$score2[mydata$gender == "f"]<-2.5*mydata$q1 + mydata$q2Warning message: number of items to replace is not a multiple of replacement length> mydata$score1[mydata$gender == "m"]<-3*mydata$q1 + mydata$q2Warning message: number of items to replace is not a multiple of replacement length> mydata$score2[mydata$gender == "m"]<-3.5*mydata$q1 + mydata$q2Warning message: number of items to replace is not a multiple of replacement length>-----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).
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).
Good idea. I'm still getting used to how flexible R is on substitutions like that! -Bob -----Original Message----- From: Leeds, Mark (IED) [mailto:Mark.Leeds at morganstanley.com] Sent: Friday, November 24, 2006 10:20 PM To: Muenchen, Robert A (Bob) Subject: RE: [R] Multiple Conditional Tranformations You could set temp<-which(my$gender[my$gender == "f"]) and then temp will have the female indices and Then you could just put temp everywhere instead of the statement but I think that's the best you can do. Definitely, someone will reply and there may be a shorter way that I am unaware of. -----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 10:09 PM To: r-help at stat.math.ethz.ch Subject: Re: [R] Multiple Conditional Tranformations 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. -------------------------------------------------------- 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).