Script for fetching a list of "end-positions" from a MySQL database
The comments are good; I copy them here so this question has a "proper" answer, and add some my own:


  Just off the top of my head: 1) Don't name things c. It's a very important base R function. 2) Now that you've renamed it, don't sort it twice. Sort it once, and then use head and tail. –  joran


and 


  Also, change c[i,1]&lt;-actionlist$OpenTime[i] to c[,1]&lt;-actionlist$OpenTime and move it out of the loop. –  Richie Cotton


and


  Also, look at ?Rprof to know how you can check which part of your function is slowing everything down. –  Joris Meys


This last is important; if you don't know where the code is slow, you don't know where to apply effort to speed it up.

My comments are that, if you have a function that is doing some work, all the information that it needs should generally be passed in as arguments. It took me a bit to figure out what c and z (after I overcame the cognitive dissonance of thinking c[i,1] was itself an error).