The OP has requested to remove all rows from a large data set of monthly values which do not make up a full year. Although the solution suggested by Wen seems to be working for the OP I would like to suggest a more robust approach.
Wen's solution counts the number of rows per year assuming that there is exactly one row per month. It would be more robust to count the number of unique months per year in case there are duplicate entries in the production data set.
(From my experience, one cannot be careful enough when dealing with production data and check all assumptions).
library(data.table)
# count number of unique months per year,
# keep only complete years, omit counts
# result is a data.table with one column Year
full_years <- DT[, uniqueN(month(Date)), by = Year][V1 == 12L, -"V1"]
full_years
Year
1: 2010
# right join with original table, only rows belonging to a full year will be returned
DT[full_years, on = "Year"]
Date Return Year
1: 2010-01-01 0.83293 2010
2: 2010-02-01 0.18279 2010
3: 2010-03-01 0.19416 2010
4: 2010-04-01 0.38907 2010
5: 2010-05-01 0.37834 2010
6: 2010-06-01 0.64010 2010
7: 2010-07-01 0.62079 2010
8: 2010-08-01 0.42128 2010
9: 2010-09-01 0.43117 2010
10: 2010-10-01 0.42307 2010
11: 2010-11-01 -0.19940 2010
12: 2010-12-01 -0.22520 2010
Note that this approach avoids to add a count
column to each row of a potentially large data set.
The code can be written more concisely as:
DT[DT[, uniqueN(month(Date)), by = Year][V1 == 12L, -"V1"], on = "Year"]
It is also possible to check the data for any duplicate months, e.g.,
stopifnot(all(DT[, .N, by = .(Year, month(Date))]$N == 1L))
This code counts the number of occurrences for each year and month and halts execution when there is more than one.