jak123
June 21, 2021, 8:31pm
1
Hi everyone
I got these daily sales ts, that I want to make monthly:
I got data from 18-01-01 to 20-10-31 with multiple duplicates of the same date since it is split between different customers.
(expenses <- data_frame(
date=seq(as.Date("2018-01-01"), as.Date("2020-10-31"), by=1),
Sales=rgamma(length(date), shape = 2, scale = 20)))
expenses %>% group_by(month=floor_date(df$Date, "month")) %>%
summarize(amount=sum(df$Sales))
So in short what I want is monthly sales data, I'm using a mac with the latest version of R-studio
Thanks for helping
rdr
June 22, 2021, 12:54am
2
One way to approach this would be to add a salesMonth column to your data frame using the floor_date
function from lubridate
. Then you can use dplyr
to group by the salesMonth and summarize
that variable with sum
.
Here's a simple using a data.frame I constructed called dailySales
:
library(dplyr)
library(lubridate)
dailySales <- data.frame(
saleDate = as.Date(c("2020-01-10", "2020-01-12", "2020-01-12", "2020-01-15", "2020-02-10", "2020-02-25")),
saleAmount = c(100, 100, 200, 100, 50, 100)
)
dailySales <- dailySales %>%
mutate(month = floor_date(dailySales$saleDate, "month"))
dailySales %>%
group_by(month) %>%
summarize(total = sum(saleAmount))
jak123
June 22, 2021, 7:31am
3
Hi rdr,
Thanks alot for your help, the problem is solved! but when make the monthly data into a timeseries and plot it I get this odd looking graph!
image|690x439
can u explain why it plots the straight line above the actual plot?
head(ts_monthlydata)
Best Jakob
1 Like
rdr
June 22, 2021, 2:29pm
4
Jakob -- Post the code for your graph and perhaps someone can help with that.
For me, picking up from above, once I have the monthly totals, I can create a quick bar chart of monthly sales using ggplot():
library(ggplot2)
monthly <- dailySales %>%
group_by(month) %>%
summarize(total = sum(saleAmount))
ggplot(monthly) +
geom_col(aes(x = month, y = total))
Hope this helps.
-- Robert
jak123
June 22, 2021, 6:44pm
5
It does, and thanks a lot Robert, it was really helpful
1 Like
system
Closed
June 29, 2021, 6:45pm
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.