Search code examples
rdplyrtidyversetibble

How can I combine NaN <character> and a numeric value under the same column in a tibble?


I have a tibble dim[1x17] from each run of the model. Since I run the model 100 times, I got 100 same structure seperate tibbles and I want to combine them with rows. All column names of tibbles are same. You can find an example of my tibble, below.

My problem is for example; AUC column should be dbl and the value of this column in some tibble is NaN which is assigned as character, the others are numeric. Therefore, I couldn't combine them.

I'm newbie in R, can someone help me to do this?

Thank you!

# A tibble: 1 × 17
#>   cv_metric_AUC logLoss   AUC prAUC Accuracy Kappa    F1 Sensi…¹ Speci…² Pos_P…³
#>           <dbl>   <dbl> <dbl> <dbl>    <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>
#> 1         0.622   0.684 0.647 0.606    0.590 0.179   0.6     0.6   0.579     0.6
#> # … with 7 more variables: Neg_Pred_Value <dbl>, Precision <dbl>, Recall <dbl>,
#> #   Detection_Rate <dbl>, Balanced_Accuracy <dbl>, method <chr>, seed <dbl>,
#> #   and abbreviated variable names ¹​Sensitivity, ²​Specificity, ³​Pos_Pred_Value

Solution

  • @akrun beat me to it, but you can convert any character NaN to logical. Here is an example:

    library(tidyverse)
    
    #example
    model_list <- list()
    cyls <- c(6, 4, 8, 12)
    for(i in seq_along(cyls)){
      run <- tryCatch({
        mod <- lm(mpg~hp, data = mtcars[mtcars$cyl == cyls[[i]],])
        tibble(cyl = cyls[[i]], r2 = summary(mod)$r.squared)
      }, error = \(e) tibble(cyl = cyls[[i]], r2 = "NaN"))
      
      model_list[[i]] <- run
      
    }
    
    model_list
    #> [[1]]
    #> # A tibble: 1 x 2
    #>     cyl     r2
    #>   <dbl>  <dbl>
    #> 1     6 0.0161
    #> 
    #> [[2]]
    #> # A tibble: 1 x 2
    #>     cyl    r2
    #>   <dbl> <dbl>
    #> 1     4 0.274
    #> 
    #> [[3]]
    #> # A tibble: 1 x 2
    #>     cyl     r2
    #>   <dbl>  <dbl>
    #> 1     8 0.0804
    #> 
    #> [[4]]
    #> # A tibble: 1 x 2
    #>     cyl r2   
    #>   <dbl> <chr>
    #> 1    12 NaN
    
    
    #does not work
    map_dfr(model_list, ~.x)
    #> Error in `dplyr::bind_rows()`:
    #> ! Can't combine `..1$r2` <double> and `..4$r2` <character>.
    
    #convert NaN
    map_dfr(model_list, ~mutate(.x, across(where(is.character),as.logical)))
    #> # A tibble: 4 x 2
    #>     cyl      r2
    #>   <dbl>   <dbl>
    #> 1     6  0.0161
    #> 2     4  0.274 
    #> 3     8  0.0804
    #> 4    12 NA