Tena koe Greg

df$c <- 0
df[apply(df[,-3], 1, function(x) any(x %in% 3)), 3] <- 1
df[apply(df[,-3], 1, function(x) all(is.na(x))), 3] <- NA
df
   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

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
> project.org] On Behalf Of Greg Blevins
> Sent: Monday, 18 October 2010 2:04 p.m.
> To: r-help@r-project.org
> Subject: [R] Getting tripped up on NAs in trying to create new variable
> 
> 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
> gregb...@gmail.com
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@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.

The contents of this e-mail are confidential and may be subject to legal 
privilege.
 If you are not the intended recipient you must not use, disseminate, 
distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received 
this
 e-mail in error, please notify the sender and delete all material pertaining 
to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the 
individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

______________________________________________
R-help@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.

Reply via email to