Generate histograms out of dplyr pipe

  • A+
Category:Languages

I have a dataset that I want to group_by() and generate a histogram for each group. My current code is as follows:

df %>%   group_by(x2) %>%   with(hist(x3,breaks = 50)) 

This however generates a single histogram of the entirety of x3 rather than several chunks of x3 here is some example data

df = data.frame(x1 = rep(c(1998,1999,2000),9),             x2 = rep(c(1,1,1,2,2,2,3,3,3),3),             x3 = rnorm(27,.5)) 

desired output:

Generate histograms out of dplyr pipe

actual output:

Generate histograms out of dplyr pipe

 


My comment about do is dated, I guess. ?do points us to the current ?group_walk:

df %>%  group_by(x2) %>%  group_walk(~ hist(.x$x3)) 

In versions of dplyr < 0.8.0, there is no group_walk, so you can use do:

df %>%    group_by(x2) %>%   do(h = hist(.$x3)) 

Assuming you only want the side-effects of hist (printed histogram), not the returned values, you can add a %>% invisible() to the end of the chain to not print the resulting tibble.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: