Home > front end >  creating dataframe of stats from another dataframe in R
creating dataframe of stats from another dataframe in R

Time:06-23

I have the following code.

n_manu <- mpg %>% dplyr::select(manufacturer) %>% n_distinct()
n_model <- mpg %>% dplyr::select(model) %>% n_distinct()
n_year <- mpg %>% dplyr::select(year) %>% n_distinct()

I want to put this in a dataframe that looks like so: Is there a way I can do this elegantly without 3 lines of code for calculating the distinct valuse?

stat      value
n_manu    15
n_model   38
n_year    2

CodePudding user response:

library(tidyverse)

mpg %>% 
  summarise(across(c(manufacturer, model, year), n_distinct))

Gives

# A tibble: 1 × 3
  manufacturer model  year
         <int> <int> <int>
1           15    38     2

and

mpg %>% 
  summarise(across(c(manufacturer, model, year), n_distinct)) %>% 
  pivot_longer(everything(), names_to="stat")
# A tibble: 3 × 2
  stat         value
  <chr>        <int>
1 manufacturer    15
2 model           38
3 year             2

From there you can finesse "row labels" with ease.

To save the results as a dataframe, simply assign the result of the pipe to an object:

summaryStats <- mpg %>% 
  summarise(across(c(manufacturer, model, year), n_distinct)) %>% 
  pivot_longer(everything(), names_to="stat")
  • Related