Guest User

R - Custom Sort

a guest
Jun 7th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.02 KB | None | 0 0
  1. > `<.dfsc`
  2. function(c1, c2) {
  3.     return(lt_list(toList(c1),toList(c2)))
  4. }
  5.  
  6. > lt_list
  7. function(l1, l2) {
  8.     n1 <- length(l1)
  9.     n2 <- length(l2)
  10.     j = 1
  11.     while(j <= n1 && j <= n2) {
  12.         if (l1[[j]] != l2[[j]]) {
  13.             return (l1[[j]] < l2[[j]])
  14.         }
  15.         j = j + 1
  16.     }
  17.     return(n1 < n2)
  18. }
  19.  
  20. > S1_E
  21. [[1]]
  22. [[1]][[1]]
  23. [1] "(1,2,C,a,C)"
  24.  
  25. [[1]][[2]]
  26. [1] 395
  27.  
  28. [[1]][[3]]
  29. [1] 422
  30.  
  31.  
  32. [[2]]
  33. [[2]][[1]]
  34. [1] "(1,2,C,d,C)"
  35.  
  36. [[2]][[2]]
  37. [1] 378
  38.  
  39. [[2]][[3]]
  40. [1] 422
  41.  
  42.  
  43. [[3]]
  44. [[3]][[1]]
  45. [1] "(1,2,C,a,D)"
  46.  
  47. [[3]][[2]]
  48. [1] 340
  49.  
  50. [[3]][[3]]
  51. [1] 422
  52.  
  53.  
  54. [[4]]
  55. [[4]][[1]]
  56. [1] "(1,2,B,a,C)"
  57.  
  58. [[4]][[2]]
  59. [1] 326
  60.  
  61. [[4]][[3]]
  62. [1] 422
  63.  
  64.  
  65. [[5]]
  66. [[5]][[1]]
  67. [1] "(1,2,B,b,C)"
  68.  
  69. [[5]][[2]]
  70. [1] 299
  71.  
  72. [[5]][[3]]
  73. [1] 422
  74.  
  75. > class(S1_E[[1]][[1]])
  76. [1] "dfsc"
  77. > class(S1_E[[2]][[1]])
  78. [1] "dfsc"
  79.  
  80. > S1_E[[1]][[1]] < S1_E[[1]][[1]]
  81. [1] FALSE
  82. > S1_E[[1]][[1]] < S1_E[[2]][[1]]
  83. [1] TRUE
  84. > S1_E[[3]][[1]] < S1_E[[2]][[1]]
  85. [1] TRUE
  86. > S1_E[[3]][[1]] < S1_E[[4]][[1]]
  87. [1] FALSE
Add Comment
Please, Sign In to add comment