Please find below the data and the code
structure(list(Id = 1:15, nos = c(4L, 2L, 3L, 2L, 3L, 2L, 2L,
3L, 1L, 5L, 2L, 1L, 2L, 1L, 1L), location = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 5L, 5L, 1L, 2L, 2L, 6L, 3L, 4L, 4L), .Label = c("Aus",
"Ind", "KA", "LA", "ML", "PA"), class = "factor"), grade = c(3L,
3L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 3L, 3L, 4L, 3L, 3L, 4L), Date_Created = structure(c(3L,
4L, 5L, 7L, 9L, 9L, 9L, 10L, 1L, 2L, 6L, 6L, 6L, 6L, 8L), .Label = c("10/14/2016",
"10/31/2016", "3/1/2016", "3/15/2016", "3/21/2016", "4/13/2017",
"5/25/2016", "6/29/2017", "7/29/2016", "8/4/2016"), class = "factor")), class = "data.frame", row.names = c(NA,
-15L))
library(lubridate)
data$Date_Created <- as.Date(data$Date_Created, "%m/%d/%Y") # convert to date
data$less6month <- data$Date_Created %m-% months(6) # subtract 6 months
data$count <- 0 # initialise counter
i <- 1 # for testing
for (i in 1:nrow(data)){ # loop through rows
compare all rows to row i
compare <- (data$location==data$location[i]) &
(data$grade==data$grade[i]) &
(data$Date_Created >=data$less6month[i])&
(data$Date_Created <= data$Date_Created[i])
count number of TRUE results
data$count[i] <- sum(compare)
}