'Convert column into rows repeating ID
I have the following dataset
ID <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6)
segment <- c(26,28,41,42,26,28,30,31,26,28,30,31,30,31,41,42,30,31,35,40,26,29,41,43)
tbl <- tibble(ID, segment)
I want to create different vectors with the IDs that have segments = 26 and 28, segments = 41 and 42.
For example in this case, ID 1. 2. 3 have both 26 and 28. And ID 1 and 4 have 41 and 42
Solution 1:[1]
Perhaps this helps
library(dplyr)
tbl %>%
group_by(ID) %>%
filter(all(c(26, 28) %in% segment), segment %in% c(26, 28)) %>%
pull(ID) %>%
unique
[1] 1 2 3
tbl %>%
group_by(ID) %>%
filter(all(c(41, 42) %in% segment), segment %in% c(41, 42)) %>%
pull(ID) %>%
unique
[1] 1 4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |
