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.
1) tapply will work for quantile, but the syntax was a little off: try this tapply(Cape $ ResponseTime, Cape $ Grid_ID, quantile, c(0.05, 0.95)) The fourth argument is additional parameters passed to the function FUN which here is quantile. You could also do this tapply(Cape $ ResponseTime, Cape $ Grid_ID, function(x) quantile(x, c(0.05, 0.95))) 2) See 1 3) I can do it with the simplify2array() function but I would have expected the simplify = T argument to tapply() to get the job done. Let me look into that and get back to you -- I know for sapply() simplify = T is what calls simplify2array() so I'm pondering. Thanks for spending so much time on a well-crafted question, Michael PS -- An even easier way to send data via plain text is to use dput() which creates code that can be directly pasted into an R session to replicate your data. Super helpful for stuff like this On Tue, Nov 15, 2011 at 2:54 PM, Kulpanowski, David <DKulpanowski at leegov.com> wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
David:
You need to re-read ?tapply _carefully_.? Note that:
FUN the function to be applied, or NULL. In the case of functions
like +, %*%, etc., the function name must be backquoted or quoted.
Now note that in tapply(whatever, byfactor, mean), "mean" _is_ a
function.
However in tapply (whatever,byfactor, quantile(something, .9) )
"quantile(something, .9)" is not a function -- it's a function
_call_
.
So correct syntax would be:
tapply(whatever, byfactor, quantile, probs=.9) ## probs is a "..."
argument
Alternatively, using anonymous functions, you could do:
tapply(whatever, byfactor, function(x)quantile(x, probs=.9))
where unnamed ("anonymous") function has been written in place for the
FUN argument of tapply.
Cheers,
Bert
On Tue, Nov 15, 2011 at 11:54 AM, Kulpanowski, David
<DKulpanowski at leegov.com> wrote:>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm