[R] Problematic If-Else statement

David Winsemius dwinsemius at comcast.net
Fri Sep 16 17:08:55 CEST 2011


On Sep 16, 2011, at 10:36 AM, Ben Ganzfried wrote:

> Hi guys,
>
> My code (next 2 lines below) isn't doing what I'm expecting it to:
>
> tmp <-
> ifelse(uncurated 
> $days_to_tumor_recurrence=="null","norecurrence","recurrence")
> curated$recurrence_status <- tmp
>
> I want the column "recurrence_status" in my output file to have values
> "recurrence" (if the input value had a number in the
> days_to_tumor_recurrence column for the particular patient) or
> "norecurrence" (if the input value said "null" in the
> days_to_tumor_recurrence column for the particular patient).
>
> Part of my above code gives me what I want. My current output file  
> does
> indeed get "recurrence" everywhere it should say this.  But for the  
> input
> values of "null" I am getting output values of NA instead of
> "norecurrence."  I tried the following strategies to solve this and  
> was
> unsuccessful.
>
> First, I added a line in the middle of the two above lines of code,  
> with the
> following:
> tmp[tmp==NA] <- "norecurrence"
>
> This did not work.
>
> Second, I changed the first line of my code to:
> tmp <-
> ifelse(uncurated 
> $days_to_tumor_recurrence==NA,"norecurrence","recurrence")

I do not know why your original did not succeed but this code should  
have been written:

  tmp <-
ifelse(is,na(uncurated 
$days_to_tumor_recurrence),"norecurrence","recurrence")

Nothing ever "equals" NA. NA is not less than anything. It is not TRUE- 
ly greater than anything. However, in one sense it is both less than  
anything as well as greater than everything including the infinities, - 
Inf and Inf:

 > min(c(NA, 1))
[1] NA
 > max(c(NA, 1))
[1] NA
 > min(c(NA, 1, -Inf))
[1] NA
 > min(c(NA, 1, Inf))
[1] NA

Gertrude Stein is once alleged to have commented about Oakland  
California, "There's no there there." There is quite a bit there now,   
but NA still has "no there there".

>
> This was worse, as was trying:
>
> tmp <-
> ifelse(uncurated 
> $days_to_tumor_recurrence=="NA","norecurrence","recurrence")
>
> I would greatly appreciate any advice you may have. Also, why is this
> happening?
>
> Thanks a bunch,
>
> Ben
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list