Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. data = tibble(X = c("a", "b", "c", "d"),
  2. Y = c("a1", "b1", "c1", "d1"),
  3. Z = c("a2", "b2", "c2", "d2"),
  4. all = list(c("a", "c"), c("b"), c("c", "c1"), c("d", "d2")))
  5.  
  6. data %>%
  7. mutate(result = case_when(Y %in% all ~ Y,
  8. Z %in% all ~ Z,
  9. TRUE ~ "none"))
  10.  
  11. # A tibble: 4 x 5
  12. X Y Z all result
  13. <chr> <chr> <chr> <list> <chr>
  14. 1 a a1 a2 <chr [2]> none
  15. 2 b b1 b2 <chr [1]> none
  16. 3 c c1 c2 <chr [2]> none
  17. 4 d d1 d2 <chr [2]> none
  18.  
  19. # A tibble: 4 x 5
  20. X Y Z all result
  21. <chr> <chr> <chr> <list> <chr>
  22. 1 a a1 a2 <chr [2]> none
  23. 2 b b1 b2 <chr [1]> none
  24. 3 c c1 c2 <chr [2]> c1
  25. 4 d d1 d2 <chr [2]> d2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement