From: Gabor Grothendieck <ggrothendieck_at_gmail.com>

Date: Tue, 22 Jan 2008 14:08:47 -0500

R-help_at_r-project.org 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. Received on Tue 22 Jan 2008 - 19:12:33 GMT

Date: Tue, 22 Jan 2008 14:08:47 -0500

You could create a lookup table or use recode in the car package.

Another possibility is to use a logical/arithmetic expression. The following expression says that

- if A is 1 then use the first term equals the coefficient, namely 1 if B ==1 and -1 if B == 2. Also, if A is not 1 then that term is zero and can be ignored.
- if A is 2 or 99 then the second or third terms are used analogously
- otherwise no terms are selected and the expression equals zero

transform(Data, new =

(A == 1) * ((B == 1) - (B == 2)) + (A == 2) * ((B == 2) - (B == 1)) + (A == 4) * 99)

This could be reduced even more although at the expense of understandability, e.g.

transform(Data, new = ifelse(A > 2, 99 * (A == 4), (A == B) - (A != B)))

On Jan 22, 2008 12:25 PM, Dimitri Liakhovitski <ld7631_at_gmail.com> wrote:

> Hello,

*> I have 2 variables in my sample Data: Data$A and Data$B
**> Variable Data$A can assume values: 1, 2, 3, and 4.
**> Variable Data$B identifies my cases and can assume values: 1 and 2.
**>
**> I need to recode my variable Data$A into a new variable Data$new such that:
**>
**> People who are Data[Data$B %in% 1, ] are recoded like this:
**>
**> Value on Data$A Value on Data$new
**> 1 +1
**> 2 -1
**> 3 0
**> 4 99
**>
**> People who are Data[Data$B %in% 2, ] are recoded like this:
**>
**> Value on Data$A Value on Data$new
**> 1 -1
**> 2 +1
**> 3 0
**> 4 99
**>
**> I am having hard time doing this. Any help would be greatly appreciated.
**> Dimitri
**>
**> ______________________________________________
**> R-help_at_r-project.org 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_r-project.org 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. Received on Tue 22 Jan 2008 - 19:12:33 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Tue 22 Jan 2008 - 22:30:07 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*