Print data frame dimensions at each step of filtering

后端 未结 3 503
日久生厌
日久生厌 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:46

    The pipe %T>% from library magrittr was created just for this type of cases :

    library(magrittr)
    library(dplyr)
    mtcars %>%
      filter(cyl > 4)     %T>% {print(dim(.))} %>%
      filter(am == 0)     %T>% {print(dim(.))} %>%
      filter(disp >= 200) %T>% {print(dim(.))}
    

    Very easy to read and edit out in Rstudio using alt + selection if you ident as I do.

    You can also use @hrbrmstr 's function here if you don't like brackets, except you won't need the last line.


    Revisiting it months later here's an idea generalizing @hrbmst's solution so you can print pretty much what you want and return the input to carry on with the pipe.

    library(tidyverse)
    pprint <- function(.data,.fun,...){
      .fun <- purrr::as_mapper(.fun)
      print(.fun(.data,...))
      invisible(.data)
    }
    
    iris %>%
      pprint(~"hello")           %>%
      head(2)                    %>%
      select(-Species)           %>%
      pprint(rowSums,na.rm=TRUE) %>%
      pprint(~rename_all(.[1:2],toupper)) %>%
      pprint(dim)
    
    # [1] "hello"
    #    1    2 
    # 10.2  9.5 
    #   SEPAL.LENGTH SEPAL.WIDTH
    # 1          5.1         3.5
    # 2          4.9         3.0
    # [1] 2 4
    

提交回复
热议问题