A use of gsub, reshape2 and sqldf with healthcare data

Building off other industry-specific posts, I want to use healthcare data to demonstrate the use of R packages. The data can be downloaded here. To read the .CSV file in R you might read the post how to import data in R. Packages in R are stored in libraries and often are pre-installed, but reaching the next level of skill requires being able to know when to use new packages and what they contain. With that let’s get to our example.

gsub

When working with vectors and strings, especially in cleaning up data, gsub makes cleaning data much simpler. In my healthcare data, I wanted to convert dollar values to integers (ie. $21,000 to 21000), and I used gsub as seen below.

Reading the data in R from CSV file. I am naming the dataset “hosp”.

hosp <- read.csv("Payment_and_value_of_care_-_Hospital.csv")

In the code below I will remove hospitals without estimates

hospay<-hosp[hosp$Payment.category !="Not Available" & hosp$Payment.category !="Number of Cases Too Small",]

Now its time to remove the dollar signs and commas in estimate values

hospay$Payment <- as.numeric(gsub("[$,]","",hospay$Payment))
hospay$Lower.estimate <- as.numeric(gsub("[$,]", "", hospay$Lower.estimate))
hospay$Higher.estimate <- as.numeric(gsub("[$,]", "", hospay$Lower.estimate))

head(hospay$Payment)
[1] 13469 12863 12308 12222 21376 14740

reshape2

In looking at the data, I wanted to focus on the Payment estimate. So I used the melt() function that is part of reshape2. Melt allows pivot-table style capabilities to restructure data without losing values.

library(reshape2)
hosp_mel<-melt(data=hospay,id=c(2,5,9,11), measure=as.numeric(c(13)), value.name='Estimate') 

names(hosp_melt)
[1] "Hospital.name"        "State"                "Payment.measure.name" "Payment.category"     "variable"             "Estimate" 

sqldf

With my data melted, I wanted to get the average estimate for heart attack patients by state. This is a classic SQL query, so bringing in sqldf allows for that.

library(sqldf)
names(hosp_melt) [3] <- "paymentmeasurename"
hosp_est <- sqldf("select State, avg(Estimate) as Estimate 
from hosp_melt 
where paymentmeasurename = 'Payment for heart attack patients' 
group by State")

head(hosp_est)
   State  Estimate
1     AK  20987.60
2     AL  21850.32
3     AR  21758.00
4     AZ  22690.62
5     CA  22707.45
6     CO  21795.30 

If you have any question feel free to leave a comment below.

7 Comments

  1. QY
    qingye yuan January 23, 2019

    The link you used to provide the data is not working any more

    Reply
  2. MS
    Moses Githaiga Sr. January 7, 2016

    Very nice post

    Reply
  3. RP
    Ricardo Pietrobon December 20, 2015

    very nice post. another way to get all the functionality your describe would be to use dplyr, perhaps with the advantage of using a single syntax. there is a nice cheat sheet at https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf

    Reply
  4. DB
    David Backus December 19, 2015

    Thanks for the data link. I think you can read the data straight in. Here’s an example in Python, but I’ve done similar things in R.

    import pandas as pd
    url = ‘https://data.medicare.gov/api/views/c7us-v4mf/rows.csv’
    df = pd.read_csv(url)

    Reply
    1. K
      Klodian December 19, 2015

      Yes we would be interesting for R code.

      Reply
      1. SS
        Shae Selix January 5, 2016

        Simply replace the local file name in Divya’s code with the URL

        hosp_url <- read.csv("https://data.medicare.gov/api/views/c7us-v4mf/rows.csv&quot😉

        Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.