I'm trying to find the appointment cost for each one of the appointments that our leasing office had in the past year. The leads data frame is 'desired_372' and all the monthly expenses for each one of the marketing sources comes from expenses_372. My code is really simple by I keep getting the following error:
**Error in `$<-.data.frame`(`*tmp*`, "Expense_per_Appt", value = c(0, 0,  : 
  replacement has 9 rows, data has 8** 
My Code:
desired_372$Expense_per_Appt = rep(0,nrow(desired_372)) #create an empty vector
for (i in 1:nrow(desired_372)){ 
  if (desired_372$Source[i] %in% names(expenses_372)){ #check if the marketing source the lead came from is a paid one
      if(desired_372$Reg.Month[i] %in% expenses_372$Month){ #if the date the lead came in was during accounting period
        if (desired_372$Appointment[i] == "Yes"){ #check if the lead had an appointment
#finding the indices of the source's monthly fee
        row_index = which(expenses_372$Month == desired_372$Reg.Month[i])
        col_index = which(names(expenses_372) == desired_372$Source[i]) 
        # the appointment cost is calculated by dividing the total monthly cost of the source by number of appointments in this month from this source
        desired_372$Expense_per_Appt[i] = expenses_372[row_index,col_index] / length(desired_372$`Prospect Code`[desired_372$Source == desired_372$Source[i] & 
                                                                                                                  desired_372$Reg.Month == desired_372$Reg.Month[i] &
                                                                                                                    desired_372$Appointment == "Yes"])
      } else {expenses_372$Expense_per_Appt[i] = NA}
    } else {expenses_372$Expense_per_Appt[i] = NA}
  } else {expenses_372$Expense_per_Appt[i] = NA}
}
Any Idea why I'm getting this error?
 
    