[R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Dimitri Liakhovitski
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, ]

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Marc Schwartz
Dimitri Liakhovitski 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:

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Gabor Grothendieck
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

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Gabor Grothendieck
Slight correction of the English: - if A is 1 then the first term equals the coefficient of (A == 1). That is the first term equals 1 if B==1 and equals -1 if B==2. If A does not equal 1 then the first term is zero and can be ignored. - terms 2 and 3 are interpreted analogously - if A==3 (or

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Dimitri Liakhovitski
Thanks a lot, everyone! Dimitri On 1/22/08, Gabor Grothendieck [EMAIL PROTECTED] wrote: Slight correction of the English: - if A is 1 then the first term equals the coefficient of (A == 1). That is the first term equals 1 if B==1 and equals -1 if B==2. If A does not equal 1 then the first

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread hadley wickham
No one else mentioned this, but if those 99s represent missings, you should be using NA not a special numeric value. Hadley On Jan 22, 2008 5:40 PM, Dimitri Liakhovitski [EMAIL PROTECTED] wrote: Thanks a lot, everyone! Dimitri On 1/22/08, Gabor Grothendieck [EMAIL PROTECTED] wrote: Slight

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Rolf Turner
On 23/01/2008, at 2:02 PM, hadley wickham wrote: No one else mentioned this, but if those 99s represent missings, you should be using NA not a special numeric value. Amen, bro. Using ``99'' to represent a missing value is a heinous, if all too often inflicted, crime against

Re: [R] recoding one variable into another - but differently for different cases

2008-01-22 Thread Gabor Grothendieck
Note that if you do use NA rather than 99 as others have suggested then the A==4 term should use ifelse rather than multiplication since 0 * NA = NA, not 0: transform(Data, new = (A == 1) * ((B == 1) - (B == 2)) + (A == 2) * ((B == 2) - (B ==1)) + ifelse(A == 4, NA, 0)) In fact, although more