Print data frame dimensions at each step of filtering

后端 未结 3 476
日久生厌
日久生厌 2021-01-14 19:32

I am using the tidyverse to filter out a dataframe and would like a print at each step of the dimensions (or nrows) of the intermediate objects. I thought I could simply use

3条回答
  •  星月不相逢
    2021-01-14 19:54

    @akrun's idea works, but it's not idiomatic tidyverse. Other functions in the tidyverse, like print() and glimpse() return the data parameter invisibly so they can be piped without resorting to {}. Those {} make it difficult to clean up pipes after your done exploring what's going on.

    Try:

    library(tidyverse)
    
    tidydim <- function(x) {
      print(dim(x))
      invisible(x)
    }
    
    mtcars %>%
      filter(cyl > 4) %>%
      tidydim() %>% 
      filter(., am == 0) %>%
      tidydim() %>% 
      filter(., disp >= 200) %>%
      tidydim()
    

    That way your "cleanup" (i.e. not producing interim console output) canbe to quickly/easily remove the tidydim() lines or remove the print(…) from the function.

提交回复
热议问题