3 Replies Latest reply on Mar 21, 2017 11:02 AM by adam.freeman.2

    R calculation for Healthcare patient census


      This post is adjusted from a previous post of mine, trying to calculate patient population at time of arrival (not real time) found here: Heathcare: Patient Census at Arrival time.


      It seems Tableau cannot handle this kind of calculation (observation calculation, based on 2 columns of data). So I wrote the calculation in R code, but I'm unsure on how to make sure Tableau read the results back correctly. My understanding of aggregated values is lost to me. When I view underlying data, the R calculation does not show up, so I can't validate the calculation and I'm getting very flat line over time as a result (one value no matter how it is categorized).


      I've attached a mock up of the two columns of dates and below is the SCRIPT I used (though the calculated field is not saved in the .twbx file):



      'date.time=data.frame(arrival=as.POSIXct(strptime(.arg1,"%m/%d/%Y %I:%M:%OS %p")),depart=as.POSIXct(strptime(.arg2,"%m/%d/%Y %I:%M:%OS %p")))


      for (i in 1:dim(date.time)[1]){

        census[i]=dim(date.time[date.time$arrival[i]>=date.time$arrival & date.time$arrival[i]<=date.time$depart,])[1]



      ATTR([ED Arrival]),ATTR([ED Depart])



      Can anyone make the necessary code adjustment? I know the R code itself can probably be written more efficiently, but I know it works in R.

        • 1. Re: R calculation for Healthcare patient census

          I am no R integration expert by any means, but I believe you can have R dump results into a CSV. I would do this and then use that as the standard in which to validate what is imported into Tableau Desktop data.


          A web search led me to this page which explains how they validated in Tableau: http://tagteam.harvard.edu/hub_feeds/1981/feed_items/740889


          TagTeam :: “The Winner Takes It All” – Tuning and Validating R Recommendation Models Inside Tableau - R-bloggers - Stati…


          Is this what you were looking for? Or was it preferred to have something a little more systematic rather than a one-time test?

          • 2. Re: R calculation for Healthcare patient census

            Hi Adam,


            Thanks for the reply, the blog post was a pretty good read, though it doesn't really help me in the current situation. I don't have a problem with validating the code directly in R. I can run this calculation in R and excel, but the numbers would be hard coded, so the numbers would not change when having Tableau filter out odd observations. I could do my best and clean up the data outside of Tableau, but I know that next month, I will get a new data set and I will need to clean it up again. I have created calculated fields that help remove a lot of these observation and I would hope the filter will work on new data as well.  With that in respect, I would then need Tableau's R connection to calculation the census based on "clean" data.


            Back to the R code, when I run it in R, it will output a vector of the same length as the data that is inputted (a condition that must be met, stated in the R-blogger post you linked above). When I run this code via Tableau's R connection, I am definitely getting something wrong. There are medical services that some patients will be transferred to and apparently they all have a census of 6 patients. When I try to view the underlying data, the R calculation is not present. I suspect that it is not calculating the census for each patient.


            I also suspect that I'm not aggregating the data properly, but I'm uncertain on how to do it. Currently, I'm using ATTR() on the .arg's because I want to use every single clean observation. If you have any other searches, I would appreciate it. My own google searches only got me so far.

            • 3. Re: R calculation for Healthcare patient census

              Diego medrano do you know of any R gurus who could help with this?


              My experience is limited having used R once for a brief lab test in another life of mine.