Hello R help:

I have the following data:

a <- c(NA, 2, 2, 3, 2, NA)
b <- c(NA, NA, 3, NA, 1, 3)
df <- data.frame(a, b)
> df
   a  b
1 NA NA
2  2 NA
3  2  3
4  3 NA
5  2  1
6  NA 3

I want to create variable c such that if there is a 3 in either variable a
or variable b, variable c is 1(rows 3, 4 & 6 below).

If 3 not in a or b and a value appears in a or b, then c is 0(row 2 below).
If NA is present in both variables, c is NA (row 1 below).

   a  b   c
1 NA NA   NA
2  2 NA   0
3  2  3   1
4  3 NA   1
5  2  1   0
6  NA 3   1


I have tried various ifelse attempts but have not hit upon the correct
solution yet. For example,
the following syntax throws NA for row two instead of the hope for "0".

test <- ifelse(is.na(df$a) & is.na(df$b), NA,
  ifelse((!is.na(df$a) | !is.na(df$b)) & ((df$a ==3 | df$b == 3)), 1, 0))
> test
NA NA  1  1  0  1

Any help much appreciated.
-- 
Gregory L. Blevins
Office 952 944-5743
Cell 612 251 0232
gregblev@gmail.com

	[[alternative HTML version deleted]]

