替换有X行,数据在R中有Y

时间:2018-07-14 00:18:49

标签: r

我正在尝试查找租赁办公室在过去一年中进行的每项约会的约会费用。潜在客户数据框为“ desired_372”,并且每个营销来源的所有每月费用都来自expenses_372。通过不断出现以下错误,我的代码非常简单:

**Error in `$<-.data.frame`(`*tmp*`, "Expense_per_Appt", value = c(0, 0,  : 
  replacement has 9 rows, data has 8** 

我的代码:

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}
}

知道为什么会出现此错误吗?

0 个答案:

没有答案