[R] ifelse() and missing values in test conditions

Joshua Wiley jwiley.psych at gmail.com
Tue Jul 20 19:41:16 CEST 2010


Hi Mike,

Probably the simplest way from what you have done would be to just set
any NA values in the column to 0:

DF$ANYEF[is.na(DF$ANYEF)] <- 0

Alternately, you can try this.  It should work, but it is far from elegant.

DF$ANYEF <- with(DF, ifelse(
 rowSums(cbind(PSOUGHT1, PSOUGHT2, PSOUGHT3) == "ANY", na.rm = TRUE) >=1,
 PEFF, 0))

Best regards,

Josh

On Tue, Jul 20, 2010 at 9:41 AM, Hosack, Michael <mhosack at state.pa.us> wrote:
> R experts,
>
> I have been unable to get the following ifelse statement to work as desired when applied
> to my data frame.
>
> Example:
>
> DF$ANYEF <- with(DF,ifelse(PSOUGHT1=='ANY'|PSOUGHT2=='ANY'|PSOUGHT3=='ANY',PEFF,0))
>
> ##### this statement will be replicated 16 times for 16 unique _EF variables ###
>
> Basically, I want each ANYEF for each row to equal the corresponding row value for column
> PEFF if 'ANY' occurs at least once among columns PSOUGHT1, PSOUGHT2, and PSOUGHT3, and I want
> ANYEF to equal 0 if 'ANY' is not present within either PSOUGHT1 or PSOUGHT2 or PSOUGHT3. My
> ifelse statement will yield NAs rather than zeroes as desired whenever 'ANY' is not present
> and NAs are included in at least one of the PSOUGHT variables. This data frame is a small subset
> of a larger data frame.
>
> Thank you,
>
> Mike
>
>
> DF <-
> structure(list(SITE = c("EAST.AVE", "LAMPE", "LAMPE", "EAST.AVE",
> "EAST.AVE"), MM = c(5L, 5L, 5L, 9L, 9L), DD = c(19L, 23L, 23L,
> 13L, 13L), PEFF = c(11.25, 9, 8, 1.5, 8), PSOUGHT1 = c("ANY",
> "SMB", "SMB", "YP", "ANY"), PSOUGHT2 = c("LMB", NA, NA, NA, NA
> ), PSOUGHT3 = c(NA, NA, NA, NA, NA), ANYEF = c(11.25, NA, NA,
> NA, 8)), .Names = c("SITE", "MM", "DD", "PEFF", "PSOUGHT1", "PSOUGHT2",
> "PSOUGHT3", "ANYEF"), class = "data.frame", row.names = c(24L,
> 38L, 39L, 471L, 472L))
>
> ______________________________________________
> 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/



More information about the R-help mailing list