Guest User

R subset

a guest
Apr 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.85 KB | None | 0 0
  1.  
  2. set.seed(500)
  3. dat <- data.frame(c1 = rnorm(10), c2 = sample(1:3, 10, TRUE),
  4.                   c3 = sample(LETTERS[1:5], 10, TRUE))
  5. #             c1 c2 c3
  6. # 1   0.96848929  1  A
  7. # 2   1.96536781  3  A
  8. # 3   0.88632253  1  D
  9. # 4   0.03054026  2  A
  10. # 5   0.94955742  1  B
  11. # 6  -0.57673001  3  E
  12. # 7   0.72152335  3  E
  13. # 8   0.61909890  3  A
  14. # 9   0.02100582  1  E
  15. # 10  0.27485018  1  A
  16.  
  17. dat[dat$c3 %in% dat[dat$c1 >= 0.9 & dat$c2 == 1, 'c3'], ]
  18. #            c1 c2 c3
  19. # 1  0.96848929  1  A
  20. # 2  1.96536781  3  A
  21. # 4  0.03054026  2  A
  22. # 5  0.94955742  1  B
  23. # 8  0.61909890  3  A
  24. # 10 0.27485018  1  A
  25.  
  26.  
  27. library(dplyr)
  28. dat %>% filter(c3 %in% (dat %>% filter(c1 >= 0.9, c2 == 1) %>% .$c3))
  29. #           c1 c2 c3
  30. # 1 0.96848929  1  A
  31. # 2 1.96536781  3  A
  32. # 3 0.03054026  2  A
  33. # 4 0.94955742  1  B
  34. # 5 0.61909890  3  A
  35. # 6 0.27485018  1  A
Advertisement
Add Comment
Please, Sign In to add comment