andrewallenbruce / provider

Public Healthcare Provider APIs :stethoscope:
https://andrewallenbruce.github.io/provider/
Other
18 stars 2 forks source link

Helper function to transpose yearly data #37

Closed andrewallenbruce closed 3 months ago

andrewallenbruce commented 8 months ago
library(provider)
library(dplyr)
library(tidyr)

performance <- utilization_(npi = 1043477615, type = "provider") |> 
  unnest(performance) |> 
  mutate(year = as.integer(year)) |> 
  select(year, tot_hcpcs:.pymt_per_srvc)

#> # A tibble: 8 × 12
#>    year tot_hcpcs tot_benes tot_srvcs tot_charges tot_allowed tot_payment
#>   <int>     <int>     <int>     <int>       <dbl>       <dbl>       <dbl>
#> 1  2014        45       598       823      319401      42429.      33775.
#> 2  2015        54      1042      1449      551630      82729.      64720.
#> 3  2016        62       619      1000      653517     111283.      87144.
#> 4  2017        65       606       972      460677      88160.      68173.
#> 5  2018        54       505      1034      504640     102857.      80079.
#> 6  2019        55       532      1252      617797     134101.     104987.
#> 7  2020        57       650      1260      482488     106512.      81868.
#> 8  2021        58       748      1369      444671      97159.      75295.
#> # ℹ 5 more variables: tot_std_pymt <dbl>, .copay_deduct <dbl>,
#> #   .srvcs_per_bene <dbl>, .pymt_per_bene <dbl>, .pymt_per_srvc <dbl>

performance |> 
  pivot_longer(!year,
               names_to = "measure", 
               values_to = "value") |> 
  pivot_wider(names_from = year, 
              values_from = value)

#> # A tibble: 11 × 9
#>    measure            `2014`    `2015` `2016` `2017` `2018` `2019` `2020` `2021`
#>    <chr>               <dbl>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
#>  1 tot_hcpcs           45        54    6.2 e1 6.5 e1 5.4 e1 5.5 e1 5.7 e1 5.8 e1
#>  2 tot_benes          598      1042    6.19e2 6.06e2 5.05e2 5.32e2 6.5 e2 7.48e2
#>  3 tot_srvcs          823      1449    1   e3 9.72e2 1.03e3 1.25e3 1.26e3 1.37e3
#>  4 tot_charges     319401    551630    6.54e5 4.61e5 5.05e5 6.18e5 4.82e5 4.45e5
#>  5 tot_allowed      42429.    82729.   1.11e5 8.82e4 1.03e5 1.34e5 1.07e5 9.72e4
#>  6 tot_payment      33775.    64720.   8.71e4 6.82e4 8.01e4 1.05e5 8.19e4 7.53e4
#>  7 tot_std_pymt     34339.    66427.   8.93e4 6.95e4 8.37e4 1.06e5 8.26e4 7.55e4
#>  8 .copay_deduct     8654.    18009.   2.41e4 2.00e4 2.28e4 2.91e4 2.46e4 2.19e4
#>  9 .srvcs_per_bene      1.38      1.39 1.62e0 1.60e0 2.05e0 2.35e0 1.94e0 1.83e0
#> 10 .pymt_per_bene      56.5      62.1  1.41e2 1.12e2 1.59e2 1.97e2 1.26e2 1.01e2
#> 11 .pymt_per_srvc      41.0      44.7  8.71e1 7.01e1 7.74e1 8.39e1 6.50e1 5.50e1

Created on 2023-11-05 with reprex v2.0.2