[R] Seeking help with an apparently simple recoding problem

Greg Blevins
Tue, 23 Aug 2005 15:12:33 -0000

Hello,

I have struggled, for longer than I care to admit, with this seemingly simple
problem, but I cannot find a solution other than the use of long drawn out
ifelse statements.  I know there has to be a better way.  Here is stripped down
version of the situation:

I start with:
a <- c(1,0,1,0,0,0,0)
b <- c(1,1,1,1,0,0,0)
c <- c(1,1,0,1,0,0,0)

rbind(a,b,c)
  [,1] [,2] [,3] [,4] [,5] [,6] [,7]
a    1    0    1    0    0    0    0
b    1    1    1    1    0    0    0
c    1    1    0    1    0    0    0

I refer to column 3 as the target column, which at the end of the day will be NA
in all instances.

The logic involved:

1) If columns 2, 4 thru 7 do NOT include at least one '1', then recode
columns 2 thru 7 to NA and recode column 1 to code 2.

2) If columns 2, 4 thru 7 contain at least one '1', then recode column 3
to NA.

Desired recoding of the above three rows:
  [,1] 	[,2] 	[,3] 	[,4] 	[,5] 	[,6] 	[,7]
a    2    	NA    	NA    	NA   	NA   	NA    	NA
b    1    	1    	NA    	1   	0   	0    	0
c    1    	1    	NA    	1   	0   	0    	0

Thanks you.


Greg Blevins
The Market Solutions Group, Inc.

Windows XP, Version 2.1.1