0

I'm working with data from a municipal fire department. I'm trying to identify the arrival time of the first emergency vehicle for every 9-1-1 call, some calls only had one emergency vehicle, while others had more than one. Each entry is identified by a call number (CallNum), there's a dispatch time (Dispatched), an arrival time(Arrived), and a cleared time(Cleared), there are other columns, but that doesn't matter for this task. The times were entered in military style. I was able to compute this quite successfully.

Woburn2 <- WoburnFire %>% group_by(CallNum) %>%
           summarise(FirstVehicle = min(Arrived), rm.na= TRUE)

This should be the end of it, but ran into a few issues. First, when there are multiple emergency vehicles with the same arrival time, I get a blank value in the FirstVehicle column. Second, if there's a blank value among the arrival times, then the blank value will go into the FirstVehicle column.

So how can I create an if statement in the summarize function, that if the min(Arrived) value is NA return the second value, and if the Arrived values are all equal, return the Arrived time.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 02 '20 at 19:15
  • If you look at the output from this, you'll see that there is a column in your summary named `rm.na`, which would immediately be suspect. Your first problem is that that argument is really intended for the `min` function call, so it should be *inside* its parentheses. (Your second problem, as akrun answered, is that you mis-typed the argument. It is not as evident, since `min` happily uses every named argument that is not `na.rm` as its number, and `min(TRUE)` is 1.) – r2evans Mar 02 '20 at 19:21

1 Answers1

2

The rm.na should be na.rm and it should be within min

library(dplyr)
WoburnFire %>% 
       group_by(CallNum) %>%
       summarise(FirstVehicle = min(Arrived, na.rm = TRUE))
akrun
  • 874,273
  • 37
  • 540
  • 662