I would like to write code that functions similar to that of an AVERAGEIFS function in Excel. Essentially, I would like to take the average of a variable given certain conditions are met. I have not been successful in finding a similar problem online given my situation calls for a unique condition that excludes certain instances.
What I am trying to accomplish is calculating the mean of total revenue (TOTALREV) for each vessel ID (VESSEL_ID) in all survey years (SURVEY_YEAR)--Excluding the current survey year. I have tried creating an additional column called YEAR to create a conditional "not equal" as well as what my code currently depicts.
CPTWG <- CEEDCdatEXP[,c("VESSEL_ID","SURVEY_YEAR","TOTALREV")] %>%
group_by(VESSEL_ID) %>%
summarize(AVERAGEREV = mean(TOTALREV[SURVEY_YEAR != SURVEY_YEAR]))
This code should create my ideal outcome:
VESSEL_ID <- c(1,2,3,1,2,3,1,2,3)
SURVEY_YEAR <- c(2009,2009,2009,2010,2010,2010,2011,2011,2011)
TOTALREV <- c(2718,9939,4014,7019,2016,2025,3218,7727,7252)
AVERAGEREV <- c(5118.5,4871.5,4638.5,2968,8833,5633,4868.5,5977.5,3019.5)
mydata <- data.frame(VESSEL_ID,SURVEY_YEAR,TOTALREV,AVERAGEREV)