Search code examples
rdplyrmagrittr

How to pipe an output tibble into further calculations without saving the tibble as a separate object in R?


I am having a hard time manipulating a tibble output that I receive after piping (using dplyr pipe %>%) a data frame through a series of steps. This code below returns a 2 x 3 tibble ouput:

sr_df %>% group_by(ResolutionViolated) %>% tally() %>% arrange(desc(n)) %>% mutate(total = sum(n))

This gives me a count of service requests that are and aren't violated (or simply put, late). This is well and good, but I want to be able to manipulate this same tibble further without having to save the tibble as an object.

Why? Because this way, I can filter my data frame (sr_df) before this piping operations, by company/account, priority, and other factors. I am able to filter with an if function, but this filter will not have an impact on the newly created tibble object. So I am looking to do something like this:

sr_df %>% group_by(ResolutionViolated) %>% tally() %>% arrange(desc(n)) %>% mutate(total = sum(n)) %>% round(tibble[1,2]/tibble$total*100, digits = 2)

I am an R and Coding Noob. Don't hold back - I just want to learn; learn quick and learn right. Any answers are appreciated. Thank you!

I have looked at this: R: Further subset a selection using the pipe %>% and placeholder but I don't think I get it.


Solution

  • In your caase, you can further manipulate the tibble you have generated using dplyr functions.

    Note the existence of mutate_at and summarize_at, that lets you transform a set of columns with the option to select them by column position.

    This, using . as a placeholder for the tibble you are currently manipulating, and calling an anonymous function inside mutate_at, will give you the result you expect.

    sr_df %>%
      group_by(ResolutionViolated) %>%
      tally() %>% 
      arrange(desc(n)) %>% 
      mutate(total = sum(n)) %>% 
      mutate_at(.cols = c(1, 2), 
                .funs = function(column) round(column / .$total * 100, digits = 2))