我有一个以列表作为输入的函数。以下是该函数的代码:
processAndJoinTables <- function(d_f){
x.log <- d_f$log
x.sale <- d_f$sales
x.disposition <- d_f$disposition
x.disposition <- column.checker(x.disposition)
if(ncol(x.disposition) != 0){
df <- data.frame(time = character(0), user = character(0), user_disposition = character(0), stringsAsFactors = F)
for(i in 1:nrow(x.disposition)){
df[i, 'time'] <- x.disposition$last_dialed_time[i] %>% as.character()
df[i+nrow(x.disposition), 'time'] <- x.disposition$last_dialed_time_1[i] %>% as.character()
df[i+(2*nrow(x.disposition)), 'time'] <- x.disposition$last_dialed_time_2[i] %>% as.character()
df[i+(3*nrow(x.disposition)), 'time'] <- x.disposition$last_dialed_time_3[i] %>% as.character()
df[i+(4*nrow(x.disposition)), 'time'] <- x.disposition$last_dialed_time_4[i] %>% as.character()
df[i, 'user'] <- x.disposition$last_user_id[i] %>% as.character()
df[i+nrow(x.disposition), 'user'] <- x.disposition$last_user_id_1[i] %>% as.character()
df[i+(2*nrow(x.disposition)), 'user'] <- x.disposition$last_user_id_2[i] %>% as.character()
df[i+(3*nrow(x.disposition)), 'user'] <- x.disposition$last_user_id_3[i] %>% as.character()
df[i+(4*nrow(x.disposition)), 'user'] <- x.disposition$last_user_id_4[i] %>% as.character()
df[i, 'user_disposition'] <- x.disposition$last_attempted_user_disposition[i] %>% as.character()
df[i+nrow(x.disposition), 'user_disposition'] <- x.disposition$last_disposition_1[i] %>% as.character()
df[i+(2*nrow(x.disposition)), 'user_disposition'] <- x.disposition$last_disposition_2[i] %>% as.character()
df[i+(3*nrow(x.disposition)), 'user_disposition'] <- x.disposition$last_disposition_3[i] %>% as.character()
df[i+(4*nrow(x.disposition)), 'user_disposition'] <- x.disposition$last_disposition_4[i] %>% as.character()
}
new <- left_join(x.log, df, by = c('date_added' = 'time'))
new <- new[order(new$call_id),]
if(length(which(x.sale$transaction_status == 'PAYMENT_DONE')) != 0) {
payment_date <- x.sale %>% filter(transaction_status == 'PAYMENT_DONE') %>% select(added_date) %>% as.character() %>% as.POSIXct()
saleAttemptCalls <- left_join(new %>% filter(date_added <= payment_date), x.sale %>% filter(transaction_status == 'CREATED'), by = c('phone' = 'mobile_no'))
afterSalesCalls <- left_join(new %>% filter(date_added > payment_date), x.sale %>% filter(transaction_status == 'PAYMENT_DONE'), by = c('phone' = 'mobile_no'))
medplusCalls <- rbind(saleAttemptCalls,afterSalesCalls)
bulla <- medplusCalls
# bulla
} else if(length(which(x.sale$transaction_status == 'PAYMENT_DONE')) == 0) {
saleAttemptCalls <- left_join(new , x.sale , by = c('phone' = 'mobile_no'))
bulla <- saleAttemptCalls
# bulla
}
} else if (ncol(x.disposition) == 0){
tbr <- left_join(x.log, x.sale, by = c('phone' = 'mobile_no'))
bulla <- tbr %>% select(-transaction_id)
# bulla
}
# return(bulla)
}
当评估 ncol(x.disposition)!= 0 的if子句时,该函数按预期返回数据帧。但是,如果else子句为true,则 ncol(x.disposition)== 0 为TRUE,则该函数不返回任何内容。
我尝试在最后显式返回 bulla (在代码中注释掉),但这也没有解决任何问题。 我确定我在某个地方只犯了一个小错误,但无法弄清楚在哪里。