samedi 10 juillet 2021

Evaluating undefined ifelse() condition in R

I've been hung up trying to have R evaluate an ifelse() conditional response that evaluates the following expression 0^(2)*(1/0+1)), which results in "NaN" value. The code below is what i have tried, however, i cannot force it to evaluate to the desired response.

library(tidyverse)
library(dplyr)
df=data.frame(test_statistic=c(1,1.8, 2.1,  0^(2)*(1/0+1))) %>% rowwise() %>% mutate(
  
  Significance=
    ifelse(test_statistic <1.7, "test_statistic <1.7",
           ifelse(test_statistic < 2 & test_statistic >= 1.7 ,"test_statistic < 2 & test_statistic <= 1.7",
                  ifelse(test_statistic < Inf & test_statistic >= 2.0, "test_statistic < Inf & test_statistic <= 2.0", 
                         ifelse(is.na(test_statistic)==TRUE,"is.na(test_statistic)","unspecified condition")))))

the result of the ifelse() test is shown below.

# A tibble: 4 x 2
# Rowwise: 
  test_statistic Significance                                
           <dbl> <chr>                                       
1            1   test_statistic <1.7                         
2            1.8 test_statistic < 2 & test_statistic <= 1.7  
3            2.1 test_statistic < Inf & test_statistic <= 2.0
4          NaN   NA      

by changing the squared expression to the value one (1) " 1^(2)(1/0+1)))" the ifelse() statement works, however, if the expression 0^(2)(1/0+1))) the statement does not work.

I've consulted over posts and havent been successful identifying a solution. Can someone please help, provide the following desired result.

# A tibble: 4 x 2
# Rowwise: 
test_statistic Significance                                
<dbl> <chr>                                       
  1            1   test_statistic <1.7                         
2            1.8 test_statistic < 2 & test_statistic <= 1.7  
3            2.1 test_statistic < Inf & test_statistic <= 2.0
4          NaN   is.na(test_statistic)


Thanx

Aucun commentaire:

Enregistrer un commentaire