I have used R for years but run into a seemingly simple problem involving 'ifelse'. condensed code like this a=c(2,NA,NA,NA,2,2,NA,2,NA,2) b=c(NA,1,1,NA,2,2,2,2,2,2) #I want to combined a and b into c so that c would be a valid number either a or b is not missing c=ifelse(a==1|b==1,1,ifelse(a==2|b==2,2,NA)) cbind(a,b,c) a b c [1,] 2 NA NA [2,] NA 1 1 [3,] NA 1 1 [4,] NA NA NA [5,] 2 2 2 [6,] 2 2 2 [7,] NA 2 NA [8,] 2 2 2 [9,] NA 2 NA [10,] 2 2 2 look at rows 1, 7 and 9, c supposes to be 2 but got NA instead. seemingly, the second 'ifelse' doesn't perform as it supposes. my R version is 2.12.2 any suggestion
Hi, The problem is that (a == 1 | b == 1) returns some NAs. NAs are not treated as strictly TRUE or FALSE. From the documentation (see ?ifelse) "Missing values in 'test' give missing values in the result". You need to use a construct designed to force a TRUE/FALSE value OR something that handles NAs (like is.na()). HTH, Josh On Fri, Apr 8, 2011 at 10:56 AM, wgu <wgu at uab.edu> wrote:> I have used R for years but run into a seemingly simple problem involving 'ifelse'. ?condensed code like this > > ?a=c(2,NA,NA,NA,2,2,NA,2,NA,2) > ?b=c(NA,1,1,NA,2,2,2,2,2,2) > > #I want to combined a and b into c so that c would be a valid number either a or b is not missing > > c=ifelse(a==1|b==1,1,ifelse(a==2|b==2,2,NA)) > cbind(a,b,c) > > ? ? ? ? ?a ?b ?c > ?[1,] ?2 NA NA > ?[2,] NA ?1 ?1 > ?[3,] NA ?1 ?1 > ?[4,] NA NA NA > ?[5,] ?2 ?2 ?2 > ?[6,] ?2 ?2 ?2 > ?[7,] NA ?2 NA > ?[8,] ?2 ?2 ?2 > ?[9,] NA ?2 NA > [10,] ?2 ?2 ?2 > > look at rows 1, 7 and 9, c supposes to be 2 but got NA instead. seemingly, the second 'ifelse' doesn't perform as it supposes. > > my R version is 2.12.2 > > any suggestion > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
try this:> a=c(2,NA,NA,NA,2,2,NA,2,NA,2) > b=c(NA,1,1,NA,2,2,2,2,2,2) > c1 <- ifelse((!is.na(a) & a == 1) | (!is.na(b) & b == 1)+ , 1 + , ifelse((!is.na(a) & a == 2) | (!is.na(b) & b == 2) + , 2 + , NA + ) + )> cbind(a, b, c1)a b c1 [1,] 2 NA 2 [2,] NA 1 1 [3,] NA 1 1 [4,] NA NA NA [5,] 2 2 2 [6,] 2 2 2 [7,] NA 2 2 [8,] 2 2 2 [9,] NA 2 2 [10,] 2 2 2>On Fri, Apr 8, 2011 at 1:56 PM, wgu <wgu at uab.edu> wrote:> I have used R for years but run into a seemingly simple problem involving 'ifelse'. ?condensed code like this > > ?a=c(2,NA,NA,NA,2,2,NA,2,NA,2) > ?b=c(NA,1,1,NA,2,2,2,2,2,2) > > #I want to combined a and b into c so that c would be a valid number either a or b is not missing > > c=ifelse(a==1|b==1,1,ifelse(a==2|b==2,2,NA)) > cbind(a,b,c) > > ? ? ? ? ?a ?b ?c > ?[1,] ?2 NA NA > ?[2,] NA ?1 ?1 > ?[3,] NA ?1 ?1 > ?[4,] NA NA NA > ?[5,] ?2 ?2 ?2 > ?[6,] ?2 ?2 ?2 > ?[7,] NA ?2 NA > ?[8,] ?2 ?2 ?2 > ?[9,] NA ?2 NA > [10,] ?2 ?2 ?2 > > look at rows 1, 7 and 9, c supposes to be 2 but got NA instead. seemingly, the second 'ifelse' doesn't perform as it supposes. > > my R version is 2.12.2 > > any suggestion > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
On 04/09/2011 03:56 AM, wgu wrote:> I have used R for years but run into a seemingly simple problem involving 'ifelse'. condensed code like this > > a=c(2,NA,NA,NA,2,2,NA,2,NA,2) > b=c(NA,1,1,NA,2,2,2,2,2,2) > > #I want to combined a and b into c so that c would be a valid number either a or b is not missing > > c=ifelse(a==1|b==1,1,ifelse(a==2|b==2,2,NA)) > cbind(a,b,c) > > a b c > [1,] 2 NA NA > [2,] NA 1 1 > [3,] NA 1 1 > [4,] NA NA NA > [5,] 2 2 2 > [6,] 2 2 2 > [7,] NA 2 NA > [8,] 2 2 2 > [9,] NA 2 NA > [10,] 2 2 2 > > look at rows 1, 7 and 9, c supposes to be 2 but got NA instead. seemingly, the second 'ifelse' doesn't perform as it supposes. > > my R version is 2.12.2 > > any suggestionHi wgu, c<-ifelse(is.na(a),b,a) Jim