I'm using dplyr
and rollmean
mean to calculate a 13 Week Moving Average and Growth rates. The following works:
NEW_DATA <- DATA %>%
select(CAT, Inventory_Amount, Sales, Shipments, DATE)%>%
group_by(CAT, DATE)%>%
summarise(
INVENTORY = sum(Inventory_Amount),
SO = sum(Sale),
SI = sum(Shipments)
) %>%
arrange(CAT, DATE)%>%
mutate(SO_13WK_AVG = rollmean(x = SO, 13, align = "right", fill = NA ),
GROWTH = round(((SO - lag(SO, 52)) / lag(SO, 52)) *100,2))
This codes adds two new columns "SO_13WK_AVG" (the 13 week sales average) and Growth (YoY Growth Rate for Sales)
When I try to select an additional variable from the original dataframe to include in the new summarized dataframe, the values for the new variables being created all turn to NA's. The following code generates NA's for the SO_13WK_AVG and GROWTH (all I've done is selected the "WK" variable:
NEW_DATA <- DATA %>%
select(CAT, Inventory_Amount, Sales, Shipments, DATE, WK)%>%
group_by(CAT, DATE, WK)%>%
summarise(
INVENTORY = sum(Inventory_Amount),
SO = sum(Sale),
SI = sum(Shipments)
) %>%
arrange(CAT, DATE)%>%
mutate(SO_13WK_AVG = rollmean(x = SO, 13, align = "right", fill = NA ),
GROWTH = round(((SO - lag(SO, 52)) / lag(SO, 52)) *100,2))
I searched stackoverflow and one found one thread that seems related:
Group/Mutate only returns NA and not an average
This thread suggests using na.rm = TRUE
to remove NA values from calculations. However as far as I can tell I don't have any missing values. Any help / commentary is appreciated.