[R] using tapply() with the quantile function?

Kulpanowski, David DKulpanowski at leegov.com
Tue Nov 15 20:54:17 CET 2011


Hi:

Summary:
I am trying to determine the 90th percentile of ambulance response times for groups of data.

Background:
A fire chief would like to look at emergency response times at the 90th percentile for 1 kilometer grids in Cape Coral, Florida. I have mapped out ambulance response times on a GIS map. Then I superimpose a regularly-spaced grid over the response times and spatially join the ambulance responses with the grids. Therefore each emergency incident has a grid ID and a response time. This is exported out as a text file and read into R.

Using R I issue the command "tapply(Cape $ ResponseTime, Cape $ Grid_ID, mean)" and this gives me the mean average of the response times for each 1 kilometer grid. This returns a result. It is not in the format I wanted but I can work on that as soon as I get the percentile function working. I am hoping to get a list which I can write to a text file so I can join the data back into my GIS based on the Grid ID. For example:

Grid_ID, MeanAverageResponseTime
1848, 450       (or some number)
1849, 470
1850, 389
etc
etc

Problem:
I am expecting that this command will give me the 90th percentile "tapply(Cape, Cape $ Grid_ID, quantile(Cape $ ResponseTime, 0.9))". However the error message that is returned is: "Error in match.fun(FUN)  : 'quantile(Cape$Responsetime, 0.9)' is not a function, character or symbol.
What I am hoping to get back is the following:

Grid_ID, 90thPercentileResponseTime
1848, 430       (or some number)
1849, 441
1850, 360
etc
etc
This would then be joined in my GIS map by the Grid_ID and I could then make a map showing the variation of response times at the 90th percentile.

I can't get past this error message.
Question 1.) Why would tapply work for mean but not for quantile?
Question 2.) What is the correct syntax?
Question 3.) How do I get the results to look like a comma delimited list as shown above?

Snap shot of data to play with:

Grid_ID, ResponseTime
1848, 429
1848, 122
1848, 366
1848, 311
1848, 337
1848, 245
1848, 127
1848, 596
1848, 356
1848, 239
1848, 159
1848, 366
1848, 457
1848, 145
1848, 198
1848,  68
1848, 224
1848, 226
1849, 592
1849, 424
1849, -52
1849, 196
1849, 194
1850, 351
1854, 316
1855, 650
1858, 628
1858, 466
1861, 133
1861, 137
1871, 359
1872, 580
1872, 548
1874, 469

feel free to copy this raw data into a notepad text file. Name it "Cape.txt" on your C: drive. Then in the R console I am using the following to read it in:
Cape <- read.table("C:/Cape.txt", sep=",", header=TRUE)

thanks

David Kulpanowski
Database Analyst
Lee County Public Safety
PO Box 398
Fort Myers, FL 33902
(ph) 239-533-3962
DKulpanowski at Leegov.com
Latitude 26.528843
Longitude -81.861486


Please note: Florida has a very broad public records law. Most written communications to or from County Employees and officials regarding County business are public records available to the public and media upon request. Your email communication may be subject to public disclosure.

Under Florida law, email addresses are public records. If you do not want your email address released in response to a public records request, do not send electronic mail to this entity.  Instead, contact this office by phone or in writing.



More information about the R-help mailing list