[R] ifelse problem
Charilaos Skiadas
cskiadas at gmail.com
Fri Dec 21 01:53:11 CET 2007
To check for NA, use is.na. For instance your second ifelse should read:
ifelse(is.na(Sheet1$Claims),0,Sheet1$Claims))
Converting Sheet1$Claims to character doesn't have the effect you
think it does. NA is still NA, it does not become "NA". Try for
instance:
as.character(NA)
as.character(NA) == "NA"
Haris Skiadas
Department of Mathematics and Computer Science
Hanover College
On Dec 20, 2007, at 10:26 AM, Randy Walters wrote:
>
> Could someone help me with the following code snippet. The results
> are not
> what I expect:
>
>> Sheet1$Claims[1:10]
> [1] NA 1 2 NA NA NA NA NA NA NA
>
>> Sheet1[1:10,"SubmissionStatus"]
> [1] Declined Bound Bound Bound Bound Bound
> Declined
> Dead Declined
> [10] Not Taken
> Levels: Bound Dead Declined Not Taken
>
>> Sheet1$Claimsnum <- NA
>> Sheet1$Claimsnum <- ifelse(Sheet1$SubmissionStatus != "Bound",99999,
> +
> ifelse(as.character(Sheet1$Claims)=="NA",0,Sheet1$Claims))
>>
>> Sheet1$Claimsnum[1:10]
> [1] 99999 1 2 NA NA NA 99999 99999 99999 99999
>>
>
> Here is Str(Sheet1)
>
> $ Claims : num NA 1 2 NA NA NA NA NA NA NA ...
> $ SubmissionStatus : Factor w/ 4 levels
> "Bound","Dead",..: 3 1 1
> 1 1 1 3 2 3 4 ...
>
>
> I would expect Sheet1$Claimsnum[4] to be 0, since the true
> condition of the
> 2nd ifelse evaluations to 0.
> Without the "as.character" the results are still not the way I want
> them:
>
>> Sheet1$Claimsnum <- ifelse(Sheet1$SubmissionStatus != "Bound",99999,
> + ifelse(Sheet1$Claims==NA,0,Sheet1$Claims))
>> Sheet1$Claimsnum[1:10]
> [1] 99999 NA NA NA NA NA 99999 99999 99999 99999
>>
>
> Much thanks!,
>
> Randy
>
>
>
> --
> View this message in context: http://www.nabble.com/ifelse-problem-
> tp14438449p14438449.html
> Sent from the R help mailing list archive at Nabble.com.
>
More information about the R-help
mailing list