[R] using tapply() with the quantile function?
DKulpanowski at leegov.com
Tue Nov 15 20:54:17 CET 2011
I am trying to determine the 90th percentile of ambulance response times for groups of data.
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:
1848, 450 (or some number)
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:
1848, 430 (or some number)
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:
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)
Lee County Public Safety
PO Box 398
Fort Myers, FL 33902
DKulpanowski at Leegov.com
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