Guest User

Untitled

a guest
Nov 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. df <- data.frame(
  2. gr=gl(3,5),
  3. percent = c(seq(0.1, 0.5,0.1), seq(-0.5,-0.1,0.1), seq(0.1, 0.5,0.1)),
  4. per=rep(c(0.25,-0.25,0.25),each=5),
  5. x=c(c(0,0,0,1,2), c(0,0.1,0,1,2),c(1,1,0,0.1,0)))
  6.  
  7. > df
  8. gr percent per x
  9. 1 1 0.1 0.25 0.0
  10. 2 1 0.2 0.25 0.0
  11. 3 1 0.3 0.25 0.0
  12. 4 1 0.4 0.25 1.0
  13. 5 1 0.5 0.25 2.0
  14. 6 2 -0.5 -0.25 0.0
  15. 7 2 -0.4 -0.25 0.1
  16. 8 2 -0.3 -0.25 0.0
  17. 9 2 -0.2 -0.25 1.0
  18. 10 2 -0.1 -0.25 2.0
  19. 11 3 0.1 0.25 1.0
  20. 12 3 0.2 0.25 1.0
  21. 13 3 0.3 0.25 0.0
  22. 14 3 0.4 0.25 0.1
  23. 15 3 0.5 0.25 0.0
  24.  
  25. data_manip <- function(x,per,percent){
  26.  
  27. if(all(percent>0)&all(head(x,3)==0)&!isTRUE(per<percent&any(diff(x>0)))){
  28. "Good"
  29. }
  30. else
  31.  
  32. if(all(percent<0)&isTRUE(per<percent&any(diff(x>0)))){
  33. "Dirty"
  34. }
  35. else
  36.  
  37. if(all(percent>0)&isTRUE(per>percent&any(diff(x>0)))){
  38. "Dirty"
  39. }
  40. else
  41. NA
  42. }
  43.  
  44. library(dplyr)
  45. df %>%
  46. group_by(gr)%>%
  47. do(data.frame(.,eastwood=data_manip(.$x,.$per,.$percent)))
  48.  
  49.  
  50.  
  51.  
  52. # A tibble: 15 x 5
  53. # Groups: gr [3]
  54. gr percent per x eastwood
  55. <fctr> <dbl> <dbl> <dbl> <fctr>
  56. 1 1 0.1 0.25 0.0 Good
  57. 2 1 0.2 0.25 0.0 Good
  58. 3 1 0.3 0.25 0.0 Good
  59. 4 1 0.4 0.25 1.0 Good
  60. 5 1 0.5 0.25 2.0 Good
  61. 6 2 -0.5 -0.25 0.0 <NA>
  62. 7 2 -0.4 -0.25 0.1 <NA>
  63. 8 2 -0.3 -0.25 0.0 <NA>
  64. 9 2 -0.2 -0.25 1.0 <NA>
  65. 10 2 -0.1 -0.25 2.0 <NA>
  66. 11 3 0.1 0.25 1.0 <NA>
  67. 12 3 0.2 0.25 1.0 <NA>
  68. 13 3 0.3 0.25 0.0 <NA>
  69. 14 3 0.4 0.25 0.1 <NA>
  70. 15 3 0.5 0.25 0.0 <NA>
Add Comment
Please, Sign In to add comment