0

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?

Hack-R
  • 22,422
  • 14
  • 75
  • 131
eliran
  • 21
  • 1
  • 2
  • 1
    Welcome to StackOverflow! For code debugging please always ask with a [reproducible](https://stackoverflow.com/q/5963269/1422451) example per the [MCVE](https://stackoverflow.com/help/mcve) and [`r`](https://stackoverflow.com/tags/r/info) tag description, with the desired output. You can use `dput()`, `reprex::reprex()` or built-in data sets for reproducible data. – Hack-R Jul 14 '18 at 00:22
  • Seems like your trying to code your own version of `merge`? If you share some sample data we can help you with a better method - this looks like a mess. `merge(expenses_372, desired_372, all.x = TRUE)` might be what you need, maybe with a little aggregation afterwards. – Gregor Thomas Jul 14 '18 at 00:27
  • Lokks like the error is coming from just the first line. Can you point out which line of code the error is originating from? – swiftg Jul 14 '18 at 00:37
  • Not really. But I ran the same calculations for Cost/Lead and it ran smoothly: – eliran Jul 14 '18 at 00:44
  • The only difference between cost/lead and cost/appointment calculations is one IF statement: when calculating cost/lead I made sure there was at least one lead that came for this source in this month. in the cost/appointment calculation I instead made sure the the ith lead had an appointment as illustrated above. – eliran Jul 14 '18 at 00:49

0 Answers0