Skip to content

Instantly share code, notes, and snippets.

@keithmcnulty
Created July 16, 2020 11:42
Show Gist options
  • Select an option

  • Save keithmcnulty/4f52102b99c5d8cb8906b7b2a6def87a to your computer and use it in GitHub Desktop.

Select an option

Save keithmcnulty/4f52102b99c5d8cb8906b7b2a6def87a to your computer and use it in GitHub Desktop.
mtcars %>%
dplyr::nest_by(cyl) %>%
dplyr::summarise(
coefs = lm(mpg ~ disp + drat + carb, data = data)$coefficients,
names = names(lm(mpg ~ disp + drat + carb, data = data)$coefficients)
) %>%
tidyr::pivot_wider(names_from = "names", values_from = "coefs",
names_glue = "{names}_coefficient" )
#> # A tibble: 3 x 5
#> # Groups: cyl [3]
#> cyl `(Intercept)_coeffic… disp_coefficient drat_coefficient carb_coefficient
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 52.0 -0.187 -2.73 3.45
#> 2 6 1.79 0.0479 1.96 0.623
#> 3 8 19.6 -0.0179 1.38 -0.743
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment