silent_hunter
silent_hunter

Reputation: 2508

Convert daily data into weekly

I have one data set with data from stores sales.All this data are sorted by days in period from 2017-01-03 until 2017-02-28.You can see data with code below.

Retail_data<-structure(list(Date = structure(c(17169, 17170, 17171, 17175, 
                                               17176, 17177, 17178, 17179, 17182, 17183, 17184, 17186, 17189, 
                                               17190, 17191, 17192, 17193, 17196, 17197, 17198, 17199, 17200, 
                                               17203, 17204, 17205, 17206, 17207, 17210, 17211, 17212, 17213, 
                                               17214, 17217, 17218, 17219, 17220, 17221, 17224, 17225), class = "Date"), 
                            Total = c(159.819319574035, 100.513902446432, 100.671026272139, 
                                      89.636183769411, 83.6930701040745, 50.3122378901428, 168.724058301167, 
                                      238.800990976061, 174.047961286308, 84.90480213602, 96.6327944162119, 
                                      198.946303771984, 105.202376612078, 75.2787425638002, 104.345665072145, 
                                      79.3962021603985, 96.9288749986742, 109.459300949634, 98.8901916978113, 
                                      116.830828411901, 160.725242652099, 144.215440635792, 165.263571423151, 
                                      74.8805575755291, 81.7886071569258, 118.973870995785, 166.751119591335, 
                                      146.121855191265, 141.303447982398, 339.790780448282, 120.55636917013, 
                                      111.891755247442, 127.68032568788, 100.554207486355, 101.004546687007, 
                                      107.784324411847, 87.4254774508288, 156.928775220726, 118.774701116953
                            )), row.names = c(NA, -39L), class = c("tbl_df", "tbl", "data.frame"
                            ))

So next step is converted all this data from daily data into weekly data.

DATE_MATRIX<-seq.Date(as.Date("2017-01-03"), as.Date("2017-02-28"), by = "day")
Sys.setlocale("LC_TIME", "English")
DATE_MATRIX1<-weekdays(DATE_MATRIX)
library(lubridate)
library(data.table)

DATE_MATRIX_FINAL<-data.frame(Date = DATE_MATRIX, Weekdays = DATE_MATRIX1,week_number=week(DATE_MATRIX),Year= lubridate::year(DATE_MATRIX))
View(DATE_MATRIX_FINAL)

I try with this code but unfortunately this code start to count week from Tuesday until Monday.This kind of count is no good and I want to fix it in order to start to count week from Monday until Sunday.So can anybody help me with this code ? P.S Data with this data set is not merged.I will manage this after.

Upvotes: 0

Views: 571

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389325

You can get the week number and then sum the Total values in each week.

aggregate(Total~year_week, 
               transform(Retail_data, year_week = format(Date, "%Y %V")), sum)

#  year_week    Total
#1   2017 01 361.0042
#2   2017 02 631.1665
#3   2017 03 554.5319
#4   2017 04 461.1519
#5   2017 05 630.1210
#6   2017 06 607.6577
#7   2017 07 859.6642
#8   2017 08 524.4489
#9   2017 09 275.7035

Using dplyr :

library(dplyr)
Retail_data %>% 
  group_by(year_week = format(Date, "%Y %V")) %>%  
  summarise(Total = sum(Total))

Upvotes: 3

Related Questions