Question

I am trying to produce a barplot that shows each of the four surgeons on the x-axis and the sum of each one's surgery durations on the y-axis, help is much appreciated. "Surgeon" is character type and "Duration" is 'times' type from the chron package.

Sample Data:

Surgeon Duration
Jack    00:22:00
Ben     00:34:00
Charles 03:12:00
Hank    01:15:00
Jack    00:59:00
Ben     00:44:00
Hank    01:46:00
Hank    00:32:00
Ben     00:49:00
Ben     06:19:00
Charles 07:02:00
Jack    00:58:00
Jack    00:21:00
Was it helpful?

Solution

Assuming that you have an input file called Input.txt which contains the data in your question, this will get you pretty close. For your reference, you should look at the documentation to learn how to get stuff out of the chron object.

# Aggregation
library(chron)
data = read.table("Input.txt", header=T, as.is =c(T,T))
data$Duration = chron(times=data$Duration)
processed = aggregate(Duration ~ Surgeon, data, sum)
print(processed)

# This will do total seconds, but you can change it to whatever you want to plot.
total_seconds = hours(processed$Duration) * 3600 +  minutes(processed$Duration) * 60 + seconds(processed$Duration)
barplot(total_seconds, names.arg=processed$Surgeon)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top