dimanche 29 septembre 2019

Testing number of columns in tidyverse

I wouls like to test if the number of columns of a data frame match the expected number of columns. The code should fit in a tidyverse pipeline.

However, this code did not work:

library(tidyverse)

# does not work:
mtcars %>% 
  select_if(negate(is.numeric)) %>% 
  if(ncol(.) > 0) stop("there should be no non-numeric column!")
#> Error in if (.) ncol(.) > 0 else stop("there should be no non-numeric column!"): argument is of length zero


# does work:
mtcars2 <- mtcars %>% 
  select_if(negate(is.numeric)) 
if(ncol(mtcars2) > 0) stop("there should be no non-numeric column!")

Created on 2019-09-29 by the reprex package (v0.3.0)

In appears that the "dot" (.) is not correctly evaluated/supported by ncol().

Is there a (straightforward) way to test the number of columns within the tidyverse-pipeline style?

Aucun commentaire:

Enregistrer un commentaire