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

