In short I am lookinkg for a way to calculate the unique number of forecasts from analysts for a company for a given year.
Input
dt <- data.table(CUSIP = c(1,1,1,1,2,2,2,2,2,2), Fdate = c("2000-12-31","2000-12-31","2001-12-31","2001-12-31","2000-12-31","2000-12-31","2000-12-31", "2001-12-31", "2001-12-31", "2001-12-31"), Analys = c(7649, 9845, 44283, 7649, 9845, 37856, 63528, 9845, 88254, 88254))
It looks like a question already asked Count unique values of a column by pairwise combinations of another column in R however I can't get it to work.
With the described solution of that question, the result is not what I am looking for. As it counts and presents the combinations of the fdate and analysts. However, I want the result of the number of unique analyst for a given company and forecast year.
Result from solution count unique values of a column by pairwise combinations of another column in r:
dt.cj <- merge(dt, dt, by ="Fdate", all = T, allow.cartesian = T)
dt.res <- dt.cj[Analys.x < Analys.y, .(cnt = length(unique(CUSIP.x))),by = .(Fdate, Analys.x, Analys.y)]
CUSIP = Unique ID for a company. Fdate = The date the forecast is based for. Analys = The ID of a analyst.
Desired output:
output <- data.table(Reg.ID = c(1,1,2,2), Location = c("2000-12-31","2001-12-31"), Count = c(2,2,3,2))