Reputation: 105
newbie R question:
So say I have a dataframe with 3 columns: id, date, and value.
How do I capture, for each id, if they have values that are different but only if the dates are different.
For example (below), id 1 would be a miss here (different value but same date), but id 2 would be a hit (different value on different dates). Id 3 would be a miss since the values don't differ.
id date value
1 1/1/2000 A
1 1/1/2000 B
2 1/1/2000 A
2 1/1/1999 B
3 1/1/2000 A
3 1/1/1999 A
Upvotes: 2
Views: 1212
Reputation: 886948
After grouping by 'id', check whether there are more than one unique 'date' as well as on 'value' column and pass that in filter
library(dplyr)
df1 %>%
group_by(id) %>%
filter(n_distinct(date) > 1, n_distinct(value) > 1)
-output
# A tibble: 2 x 3
# Groups: id [1]
# id date value
# <int> <chr> <chr>
#1 2 1/1/2000 A
#2 2 1/1/1999 B
Or with anyDuplicated
df1 %>%
group_by(id) %>%
filter(!anyDuplicated(date), !anyDuplicated(value))
# A tibble: 2 x 3
# Groups: id [1]
# id date value
# <int> <chr> <chr>
#1 2 1/1/2000 A
#2 2 1/1/1999 B
df1 <- structure(list(id = c(1L, 1L, 2L, 2L, 3L, 3L), date = c("1/1/2000",
"1/1/2000", "1/1/2000", "1/1/1999", "1/1/2000", "1/1/1999"),
value = c("A", "B", "A", "B", "A", "A")),
class = "data.frame", row.names = c(NA,
-6L))
Upvotes: 3