- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find monthly sales using date variable in R?
Sales analysis requires to find monthly sales mean, total, range, and often standard deviation. This is mostly required by FMCG (Fast-Moving Consumer Goods) companies because they want to track their sales on daily basis as well as monthly basis. If we have daily sales data then we need to create another column for months in an R data frame to find the monthly sales and this can be done with the help of strftime and aggregate function.
Example
Consider the below data frame −
date<-c("2020/05/05","2020/05/05","2020/05/06","2020/06/10","2020/06/25", + "2020/06/25","2020/04/15","2020/05/25","2020/03/02","2020/02/25", + "2020/03/12","2020/02/21","2020/03/04","2020/04/21","2020/05/24", + "2020/02/17","2020/06/07","2020/04/08","2020/01/25","2020/01/04", + "2020/05/20","2020/01/24","2020/04/01") sales<-c(17,25,14,19,21,24,26,28,18,25,26,18,19,25,20,17,10,18,26,27,21,24,18) df<-data.frame(date,sales) df
Output
date sales 1 2020/05/05 17 2 2020/05/05 25 3 2020/05/06 14 4 2020/06/10 19 5 2020/06/25 21 6 2020/06/25 24 7 2020/04/15 26 8 2020/05/25 28 9 2020/03/02 18 10 2020/02/25 25 11 2020/03/12 26 12 2020/02/21 18 13 2020/03/04 19 14 2020/04/21 25 15 2020/05/24 20 16 2020/02/17 17 17 2020/06/07 10 18 2020/04/08 18 19 2020/01/25 26 20 2020/01/04 27 21 2020/05/20 21 22 2020/01/24 24 23 2020/04/01 18 m.date<-strftime(df$date,"%m") Monthly_Sales_Mean<-aggregate(df$sales~m.date,FUN=mean) Monthly_Sales_Mean m.date df$sales 1 01 25.66667 2 02 20.00000 3 03 21.00000 4 04 21.75000 5 05 20.83333 6 06 18.50000 Monthly_Sales_Total<-aggregate(df$sales~m.date,FUN=sum) Monthly_Sales_Total m.date df$sales 1 01 77 2 02 60 3 03 63 4 04 87 5 05 125 6 06 74 Monthly_Sales_Range<-aggregate(df$sales~m.date,FUN=range) Monthly_Sales_Range m.date df$sales.1 df$sales.2 1 01 24 27 2 02 17 25 3 03 18 26 4 04 18 26 5 05 14 28 6 06 10 24 Monthly_Sales_SD<-aggregate(df$sales~m.date,FUN=sd) Monthly_Sales_SD m.date df$sales 1 01 1.527525 2 02 4.358899 3 03 4.358899 4 04 4.349329 5 05 5.115336 6 06 6.027714
- Related Articles
- How to find the monthly average from different date data in R?
- How to find the summary by categorical variable in R?
- How to find the mean of each variable using dplyr by factor variable with ignoring the NA values in R?
- How to calculate monthly average for time series object in R?
- How to display central limit theorem using uniform random variable in R?
- How to display average line for y variable using ggplot2 in R?
- How to display a variable as tooltip in ggplotly using R language?
- How to Calculate Working Capital using Sales Ratio Method?
- How to create a categorical variable using a data frame column in R?
- How to generate Bernoulli random variable in R?
- How to create a dummy variable in R?
- How to add a variable description in R?
- How to create an ordinal variable in R?
- How to find the date after a number of days in R?
- How to create a rank variable using mutate function of dplyr package in R?

Advertisements