Guest User

Untitled

a guest
Jun 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. just a couple of hints as you are learning the language
  2.  
  3. I do very little with apply. If necessary to process a matrix in row form, as this seems to be doing, I would define a function that did the row transformation, then apply it once.
  4.  
  5. sort takes the very useful additional parameter index=T, which will return the sorted order, obviating the need to do separate ranking.
  6.  
  7. For really sophisticated searching of sorted things, there is findInterval, which does binary search. It took me a while to find it in R. But it's incredibly nice. Here's an example of using sort (with i=T), and findInterval together.
  8.  
  9. # si.sample() :
  10. # put a arbitrarily sampled sequence of events into a regularly sampled line,
  11. # sampled at regular si intervals.
  12. # IN: assumes you have just events-points in tm1 and event values (reals) in x1.
  13. #
  14. si.sample=function(x1,tm1, si=1, t0=floor(min(tm1)), tend=ceiling(max(tm1)), fill.na=T) {
  15.  
  16. if (length(tm1) != length(x1)) stop("x1 and tm1 must be the same length!")
  17.  
  18. # insure we are sorted by tm1
  19. s=sort(tm1,i=T)
  20. tm1=s$x
  21. x1=x1[s$ix]
  22.  
  23. r=list()
  24. N = ceiling((tend - t0)/si) +1
  25. tm.new = t0 + (0:(N-1))*si
  26. a=rep(NA,N)
  27.  
  28. # binary search using findInterval which defaults to half-open on the right [a,b) intervals... but since we want
  29. # half-open on the left (a,b] intervals, we need to:
  30. # negate time, apply findInterval, un-negate time to get correct behavior at the interval boundaries.
  31.  
  32. fi=findInterval(sort(-tm1), sort(-tm.new),rightmost.closed=T) # negate time and apply findInterval
  33. fi2=1+length(tm.new)-rev(fi) # un-negating the time flip, to get correct boundary behavior.
  34.  
  35. fi2[fi2>(length(tm.new))]=1 # assign any time points past tend values back to bucket 1 to get NA-ed as well during a[1]=NA
  36. a[fi2]=x1 # correctly overwritting with later values if multiple values per interval.
  37. a[1]=NA # bucket that collects pre- t0 samples at t0 itself in tm.new; also gets samples post tend.
  38.  
  39. if (fill.na) {
  40. r$v = fill.na.with.most.recently.seen(a)
  41. } else {
  42. r$v = a
  43. }
  44. r$tm = tm.new
  45.  
  46. class(r)="regular.sample"
  47. r
  48. }
  49.  
  50. # and it uses this
  51. fill.na.with.most.recently.seen=function(x) {
  52. lastseen = x[1]
  53. for (i in 2:length(x)) {
  54. if (is.na(x[i])) {
  55. x[i]=lastseen
  56. } else {
  57. lastseen = x[i]
  58. }
  59. }
  60. x
  61. }
Add Comment
Please, Sign In to add comment