'Drop those row groups which do not have specified year values

I have the following table:-

data <- data.table(alpha = c(rep(letters[1:5], each=4), "f", "f", "g", "h", "h", rep("i", 4)),
     year = c(rep(2014:2017, 5), 2014, 2015, 2016, 2015, 2016, 2014:2017),
     values = runif(29))

data

 #    alpha year     values
 # 1:     a 2014 0.58602291
 # 2:     a 2015 0.84320915
 # 3:     a 2016 0.15504613
 # 4:     a 2017 0.81064753
 # 5:     b 2014 0.89886539
 # 6:     b 2015 0.81288998
 # 7:     b 2016 0.92019694
 # 8:     b 2017 0.64171626
 # 9:     c 2014 0.07585062
 #10:     c 2015 0.90201830
 #11:     c 2016 0.68783471
 #12:     c 2017 0.80405486
 #13:     d 2014 0.45197629
 #14:     d 2015 0.84657377
 #15:     d 2016 0.25700501
 #16:     d 2017 0.44786659
 #17:     e 2014 0.73255596
 #18:     e 2015 0.45027413
 #19:     e 2016 0.52385583
 #20:     e 2017 0.62541879
 #21:     f 2014 0.38690750
 #22:     f 2015 0.43807544
 #23:     g 2016 0.47070025
 #24:     h 2015 0.57167596
 #25:     h 2016 0.40591139
 #26:     i 2014 0.16081680
 #27:     i 2015 0.71446476
 #28:     i 2016 0.70543309
 #29:     i 2017 0.57190553

What I want to do is remove those alpha groups which do not have values for all the years, i.e., from 2014 till 2017.

The resulting data.table have only a, b, d, e and i group rows. Hence, it would look like the following data.table:-

 #    alpha year     values
 # 1:     a 2014 0.58602291
 # 2:     a 2015 0.84320915
 # 3:     a 2016 0.15504613
 # 4:     a 2017 0.81064753
 # 5:     b 2014 0.89886539
 # 6:     b 2015 0.81288998
 # 7:     b 2016 0.92019694
 # 8:     b 2017 0.64171626
 # 9:     c 2014 0.07585062
 #10:     c 2015 0.90201830
 #11:     c 2016 0.68783471
 #12:     c 2017 0.80405486
 #13:     d 2014 0.45197629
 #14:     d 2015 0.84657377
 #15:     d 2016 0.25700501
 #16:     d 2017 0.44786659
 #17:     e 2014 0.73255596
 #18:     e 2015 0.45027413
 #19:     e 2016 0.52385583
 #20:     e 2017 0.62541879
 #21:     i 2014 0.16081680
 #22:     i 2015 0.71446476
 #23:     i 2016 0.70543309
 #24:     i 2017 0.57190553


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source