How can I group days into weeks?
Do you just want the number of completed 7 day intervals since the first record?
dvec <- as.Date("2001-04-01")+0:90
dweek <- as.numeric(dvec-dvec[1]) %/% 7
dweek[1:21]
# [1] 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2
Group dates into weeks of the year, PHP
I don't think you need too much code to do it, You just need to find if the current post belongs to a specific week (number of the week in a year), then create a small logic to display additional markup based on that.
if ($posts) : ?>
<div class="all-events events-container">
<ul class="all-events-inner events-row">
<?php
// This will save the week that is being displayed
$current_week = false;
?>
<?php foreach ($posts as $post) : setup_postdata($post) ?>
<?php
// Get event start date
$start_date = get_post_meta(get_the_ID(), 'event_date_time_start', true);
// Find out this event week
$week = date('WY', strtotime($start_date));
// Check if this is the first time we are handling this week
if ($week !== $current_week) {
// Store the current week so that we don't show the same output once again until next week
$current_week = $week;
// Get the first day of this week
$dateTime = new DateTime(date("Y-m-d", strtotime($start_date)));
$monday = $dateTime->modify('Monday this week');
?>
<li class="week-commencing">W/C <?php echo $monday->format('M d'); ?></li>
<?php
}
?>
<li class="col-4 event">Blah blah stuff goes here</li>
<?php endforeach; ?>
</ul>
</div>
<?php wp_reset_postdata();
endif;
Group dates by week in R
The code below calculates the current week relative to the minimum week in the data. week2
uses modular arithmetic to make the code more concise, although the week numbers don't always line up exactly with the direct calculation of years and week numbers using lubridate
functions.
library(dplyr)
library(lubridate)
df2 %>% mutate(week = (year(Order_Date) - year(min(Order_Date)))*52 +
week(Order_Date) - week(min(Order_Date)),
week2 = (as.numeric(Order_Date) %/% 7) - (as.numeric(min(Order_Date)) %/% 7)) %>%
arrange(Order_Date)
Order_Date week week2
1 2015-10-23 0 0
2 2015-10-27 0 0
3 2015-11-01 1 1
4 2015-11-07 2 2
5 2015-11-11 2 2
6 2015-11-14 3 3
7 2015-12-17 8 8
8 2016-01-05 10 10
9 2016-01-20 12 12
10 2016-01-30 14 14
11 2016-02-18 16 17
12 2016-03-13 20 20
13 2016-03-31 22 23
14 2016-04-04 23 23
15 2016-04-15 25 25
16 2016-05-08 28 28
17 2016-05-10 28 28
18 2016-07-27 39 39
19 2016-10-01 49 49
20 2016-10-11 50 50
Group days into weeks with totals PySpark
Try with this approach using date_sub,next_day
functions in spark.
Explanation:
date_sub(
next_day(col("day"),"sunday"), //get next sunday date
7)) //substract week from the date
Example:
In pyspark:
from pyspark.sql.functions import *
df = sc.parallelize([("2009-01-03","1","0"),("2009-01-09","14","0"),("2009-01-10","61","0")]).toDF(["day","bitcoin_total","dash_total"])
df.withColumn("week_strt_day",date_sub(next_day(col("day"),"sunday"),7)).groupBy("week_strt_day").agg(sum("bitcoin_total").cast("int").alias("bitcoin_total"),sum("dash_total").cast("int").alias("dash_total")).orderBy("week_strt_day").show()
Result:
+-------------+-------------+----------+
|week_strt_day|bitcoin_total|dash_total|
+-------------+-------------+----------+
| 2008-12-28| 1| 0|
| 2009-01-04| 75| 0|
+-------------+-------------+----------+
In scala:
import org.apache.spark.sql.functions._
val df=Seq(("2009-01-03","1","0"),("2009-01-09","14","0"),("2009-01-10","61","0")).toDF("day","bitcoin_total","dash_total")
df.withColumn("week_strt_day",date_sub(next_day('day,"sunday"),7)).groupBy("week_strt_day").agg(sum("bitcoin_total").cast("int").alias("bitcoin_total"),sum("dash_total").cast("int").alias("dash_total")).orderBy("week_strt_day").show()
Result:
+-------------+-------------+----------+
|week_strt_day|bitcoin_total|dash_total|
+-------------+-------------+----------+
| 2008-12-28| 1| 0|
| 2009-01-04| 75| 0|
+-------------+-------------+----------+
Related Topics
How to Join Data from 2 Different CSV-Files in R
Greek Letters in Ggplot Strip Text
Calculating the Distance Between Polygon and Point in R
Error When Exporting Dataframe to Text File in R
System Is Computationally Singular: Reciprocal Condition Number in R
R: Clustering Results Are Different Everytime I Run
How to Flatten R Data Frame That Contains Lists
Ggplot Piecharts on a Ggmap: Labels Destroy the Small Plots
Plot Event Sequences/Event Sequences Clustering
R Cmd Check Latex Error: Fatal PDFlatex - Gui Framework Cannot Be Initialized
Error Connecting to Azure Blob Storage API from R
Cumulative Sum in a Window (Or Running Window Sum) Based on a Condition in R
Efficiently Transform Multiple Columns of a Data Frame
Geom_Smooth and Exponential Fits
How to Control the Canvas Size in Ggplot