Greg Blevins
2005-Aug-23 15:12 UTC
[R] Seeking help with an apparently simple recoding problem
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
Marc Schwartz (via MN)
2005-Aug-23 16:28 UTC
[R] Seeking help with an apparently simple recoding problem
On Tue, 2005-08-23 at 10:12 -0500, Greg Blevins wrote:> 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.You left out one key detail in the explanation, which is that the recoding appears to be done on a row by row basis, not overall. The following gets the job done, though there may be a more efficient approach:> 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)> d <- rbind(a, b, c)> d[,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> mod.row <- function(x){ if (all(x[c(2, 4:7)] == 0)) { x[2:7] <- NA x[1] <- 2 } else { x[3] <- NA } x }> y <- t(apply(d, 1, mod.row))> y[,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 HTH, Marc Schwartz