Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Muenchen, Robert A (Bob)
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: [EMAIL PROTECTED]
Web: http://oit.utk.edu/scc, 
News: http://listserv.utk.edu/archives/statnews.html
=


-Original Message-
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Saturday, November 25, 2006 12:39 AM
To: Muenchen, Robert A (Bob)
Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
  Web: http://oit.utk.edu/scc,
  News: http://listserv.utk.edu/archives/statnews.html
  =
 
 
  -Original Message-
  From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
  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: [EMAIL PROTECTED]
  [mailto:[EMAIL PROTECTED] On Behalf Of Muenchen,
Robert
  A (Bob)
  Sent: Friday, November 24, 2006 8:27 PM
  To: r-help@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

Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Gabor Grothendieck
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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
 Web: http://oit.utk.edu/scc,
 News: http://listserv.utk.edu/archives/statnews.html
 =


 -Original Message-
 From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
 Sent: Saturday, November 25, 2006 12:39 AM
 To: Muenchen, Robert A (Bob)
 Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
   Web: http://oit.utk.edu/scc,
   News: http://listserv.utk.edu/archives/statnews.html
   =
  
  
   -Original Message-
   From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
   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: [EMAIL PROTECTED]
   [mailto:[EMAIL PROTECTED] On Behalf Of Muenchen,
 Robert
   A (Bob)
   Sent: Friday, November 24, 2006 8:27 PM
   To: r-help@stat.math.ethz.ch
   Subject: [R] Multiple Conditional Tranformations
  
   Greetings,
  
  
  
   I'm learning R and I'm stuck on a basic concept: how

Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Muenchen, Robert A (Bob)
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:[EMAIL PROTECTED] 
Sent: Saturday, November 25, 2006 7:37 AM
To: Muenchen, Robert A (Bob)
Cc: r-help@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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
 Web: http://oit.utk.edu/scc,
 News: http://listserv.utk.edu/archives/statnews.html
 =


 -Original Message-
 From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
 Sent: Saturday, November 25, 2006 12:39 AM
 To: Muenchen, Robert A (Bob)
 Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
   Web: http://oit.utk.edu/scc,
   News: http://listserv.utk.edu/archives/statnews.html
   =
  
  
   -Original Message-
   From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
   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

Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Gabor Grothendieck
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) [EMAIL PROTECTED] 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:[EMAIL PROTECTED]
 Sent: Saturday, November 25, 2006 7:37 AM
 To: Muenchen, Robert A (Bob)
 Cc: r-help@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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
  Web: http://oit.utk.edu/scc,
  News: http://listserv.utk.edu/archives/statnews.html
  =
 
 
  -Original Message-
  From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
  Sent: Saturday, November 25, 2006 12:39 AM
  To: Muenchen, Robert A (Bob)
  Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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

Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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:[EMAIL PROTECTED]
  Sent: Saturday, November 25, 2006 7:37 AM
  To: Muenchen, Robert A (Bob)
  Cc: r-help@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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
   Web: http://oit.utk.edu/scc,
   News: http://listserv.utk.edu/archives/statnews.html
   =
  
  
   -Original Message-
   From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
   Sent: Saturday, November 25, 2006 12:39 AM
   To: Muenchen, Robert A (Bob)
   Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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

Re: [R] Multiple Conditional Tranformations

2006-11-25 Thread Muenchen, Robert A (Bob)
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:[EMAIL PROTECTED] 
Sent: Saturday, November 25, 2006 11:11 AM
To: Muenchen, Robert A (Bob)
Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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:[EMAIL PROTECTED]
  Sent: Saturday, November 25, 2006 7:37 AM
  To: Muenchen, Robert A (Bob)
  Cc: r-help@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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
   Web: http://oit.utk.edu/scc,
   News: http://listserv.utk.edu/archives/statnews.html
   =
  
  
   -Original Message-
   From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
   Sent: Saturday, November 25, 2006 12:39 AM
   To: Muenchen, Robert A (Bob)
   Cc: r-help@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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] wrote:
 Mark,

 I finally got that approach

Re: [R] Multiple Conditional Tranformations

2006-11-24 Thread Muenchen, Robert A (Bob)
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
 mydata
  group 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$q2
Warning message:
number of items to replace is not a multiple of replacement length 
 mydata$score2[mydata$gender == f]-2.5*mydata$q1 + mydata$q2
Warning message:
number of items to replace is not a multiple of replacement length 
 mydata$score1[mydata$gender == m]-3*mydata$q1 + mydata$q2
Warning message:
number of items to replace is not a multiple of replacement length 
 mydata$score2[mydata$gender == m]-3.5*mydata$q1 + mydata$q2
Warning message:
number of items to replace is not a multiple of replacement length 


-Original Message-
From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED] 
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
A (Bob)
Sent: Friday, November 24, 2006 8:27 PM
To: r-help@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: [EMAIL PROTECTED]
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@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

Re: [R] Multiple Conditional Tranformations

2006-11-24 Thread Muenchen, Robert A (Bob)
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: [EMAIL PROTECTED]
Web: http://oit.utk.edu/scc, 
News: http://listserv.utk.edu/archives/statnews.html
=


-Original Message-
From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED] 
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
A (Bob)
Sent: Friday, November 24, 2006 8:27 PM
To: r-help@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: [EMAIL PROTECTED]
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@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

Re: [R] Multiple Conditional Tranformations

2006-11-24 Thread Muenchen, Robert A (Bob)
Good idea. I'm still getting used to how flexible R is on substitutions
like that! -Bob


-Original Message-
From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED] 
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
A (Bob)
Sent: Friday, November 24, 2006 10:09 PM
To: r-help@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: [EMAIL PROTECTED]
Web: http://oit.utk.edu/scc,
News: http://listserv.utk.edu/archives/statnews.html
=


-Original Message-
From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
A (Bob)
Sent: Friday, November 24, 2006 8:27 PM
To: r-help@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: [EMAIL PROTECTED]
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@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R

Re: [R] Multiple Conditional Tranformations

2006-11-24 Thread Gabor Grothendieck
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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
 Web: http://oit.utk.edu/scc,
 News: http://listserv.utk.edu/archives/statnews.html
 =


 -Original Message-
 From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
 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: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
 A (Bob)
 Sent: Friday, November 24, 2006 8:27 PM
 To: r-help@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: [EMAIL PROTECTED]
 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@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

Re: [R] Multiple Conditional Tranformations

2006-11-24 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] 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) [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
  Web: http://oit.utk.edu/scc,
  News: http://listserv.utk.edu/archives/statnews.html
  =
 
 
  -Original Message-
  From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED]
  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: [EMAIL PROTECTED]
  [mailto:[EMAIL PROTECTED] On Behalf Of Muenchen, Robert
  A (Bob)
  Sent: Friday, November 24, 2006 8:27 PM
  To: r-help@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: [EMAIL PROTECTED]
  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@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