How to create a new column that identifies new value appearance in Letter column cumulatively by groups of unique combs of Year + Month?
Data sample.
require(data.table)
dt <- data.table(Letter = c(LETTERS[c(5, 1:2, 1:2, 1:4, 3:6)]),
Year = 2018,
Month = c(rep(5,5), rep(6,4), rep(7,4)))
Print.
Letter Year Month
1: E 2018 5
2: A 2018 5
3: B 2018 5
4: A 2018 5
5: B 2018 5
6: A 2018 6
7: B 2018 6
8: C 2018 6
9: D 2018 6
10: C 2018 7
11: D 2018 7
12: E 2018 7
13: F 2018 7
Result I'm trying to get:
Letter Year Month New
1: E 2018 5 TRUE
2: A 2018 5 TRUE
3: B 2018 5 TRUE
4: A 2018 5 TRUE
5: B 2018 5 TRUE
6: A 2018 6 FALSE
7: B 2018 6 FALSE
8: C 2018 6 TRUE
9: D 2018 6 TRUE
10: C 2018 7 FALSE
11: D 2018 7 FALSE
12: E 2018 7 FALSE
13: F 2018 7 TRUE
Detailed Question:
- Group1 ("E", "A", "B", "A", "B") all TRUE by default as nothing to compare with.
- Which of the letters in group2 ("A", "B", "C", "D") is not duplicated in group1.
- Then, which of letters in group3 ("C", "D", "E", "F") in not duplicated in both groups 1&2 ("E", "A", "B", "A", "B", "A", "B", "C", "D").