Reputation: 3387
I try to pro-rate the duration of a shipment to the month in which it takes place. To do this for a tibble I have created a if-structure in a 'mutate' function. When I execute this I get the following error:
Evaluation error: & not defined for "Date" objects
I have tried to search the internet for an answer, but this did not result in a resource that helped me to get the code working. I appreicate if you can assist. Below you will find a reproducable example.
Dataset:
shipments_sample <- structure(list(StartDate = structure(c(18123, 17756, 17833, 17700, 17608, 18083), class = "Date"), EndDate = structure(c(18167, 17802, 17859, 17762, 17674, 18135), class = "Date"), FromRegion = c("Europe", "Europe", "Asia", "Europe", "Asia", "Asia"), ToRegion = c("Europe", "North America", "North America", "Europe", "North America", "Europe"), FreightDays = c(42, 46, 25, 60, 60, 50), DemurrageDays = c(2, 0, 1, 2, 6, 2), Rev_Demurrage = c(15, 0, 7.5, 15, 45, 15), Rev_Freight = c(3120.36990205105, 2770.19243720274, 3263.27948309456, 3256.14131046778, 2688.29554497405, 2913.20508057298), Cost_Transport = c(2133.20515513651, 2245.89817037301, 2485.40039786172, 2357.33319394193, 2163.11768000726, 2269.96431053028), Margin = c(987.164746914546, 524.294266829736, 777.879085232832, 898.808116525851, 525.17786496679, 643.2407700427), Count_Month_Export = structure(c(18109, 17744, 17805, 17683, 17591, 18078), class = "Date"), Count_Month_Import = structure(c(18140, 17775, 17836, 17744, 17652, 18109), class = "Date")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
The code that I am using; the error comes at the end:
shipments_sample %>%
mutate(
# flights %>% filter(between(month, 7, 9))
DaysInMonths = if_else(
(StartDate >= floor_date(proratamonth,"month") & StartDate < ceiling_date(proratamonth,"month")),
as.numeric(ceiling_date(proratamonth,"month") - StartDate),
if_else(
(floor_date(proratamonth,"month") & EndDate < ceiling_date(proratamonth,"month")),
as.numeric(ceiling_date(EndDate,"day") - floor_date(proratamonth,"month")),
if_else(
(StartDate < floor_date(proratamonth,"month") & EndDate >ceiling_date(proratamonth,"month")),
as.numeric(ceiling_date(proratamonth,"month") - floor_date(proratamonth,"month")),
as.numeric(0)
)
)
),
# DaysInMonths = as.numeric(if(StartDate >= proratemonth){ceiling_date(proratemonth,"month") - StartDate}else{42}),
Duration = as.numeric(EndDate - StartDate),
PercentageInMonth = DaysInMonths/as.numeric(EndDate - StartDate),
ProRata_Rev_Freight = Rev_Freight * PercentageInMonth,
ProRata_Rev_Demurrage = PercentageInMonth * Rev_Demurrage,
ProRata_Cost_Transport = PercentageInMonth * Cost_Transport
)
Upvotes: 1
Views: 5929
Reputation: 3387
In the comments to the question camille (user:5325862) suggested to use case_when
instead of nasted if statements. The following solution worked for me:
shipments_sample %>%
mutate(
# flights %>% filter(between(month, 7, 9))
DaysInMonth = case_when(
StartDate >= floor_date(proratamonth,"month") & StartDate < ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(proratamonth,"month") - StartDate),
EndDate >= floor_date(proratamonth,"month") & EndDate < ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(EndDate,"day") - floor_date(proratamonth,"month")),
StartDate < floor_date(proratamonth,"month") & EndDate >ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(proratamonth,"month") - floor_date(proratamonth,"month")),
TRUE ~ as.numeric(0)
),
Duration = as.numeric(EndDate - StartDate),
PercentageInMonth = DaysInMonth/as.numeric(EndDate - StartDate),
ProRata_Rev_Freight = Rev_Freight * PercentageInMonth,
ProRata_Rev_Demurrage = PercentageInMonth * Rev_Demurrage,
ProRata_Cost_Transport = PercentageInMonth * Cost_Transport
)
Upvotes: 0