1

Here is my current data, using R, I sorted the dates into Year/Month/Day; however, now I wish to sort them into weeks.

library(tidyr)
library(astsa)
mydata <- read.csv("/Users/Shannon/Documents/PoliceKillingsUS.csv", header = TRUE, sep = ",", dec = ".")
mydata$ddate <- format(as.Date(mydata$date, format = "%d/%m/%y"), "%Y/%m/%d")
sort(mydata$ddate)

Ideally, the output that I would want is as follows. Though if we cannot start with week 0, that is fine.

     ddate     week
1  2015-1-2   0
2  2015-1-3   0
3  2015-1-10   1
4  2015-1-18   2

I did read this group-by-week thread in stackoverflow, but unfortunately, I kept getting errors. Group dates by week in R

Fire
  • 301
  • 1
  • 2
  • 9
  • 1
    Are you after the ISO week? if so, lubridate::isoweek("2015-01-02") will work. Leave your dates formatted as date classes though mydata$ddate <- as.Date(mydata$date, format = "%d/%m/%y") – Khaynes Dec 11 '19 at 00:11
  • @Khaynes unfortunately no, as that function would need me to type in each individual number, and my data set is far, far too large. – Fire Dec 11 '19 at 00:22
  • Does the `astsa` package have anything to do with your question? What is the trailing `sort(...)` meant to do? (It's neither changing anything nor giving relevant output?) – r2evans Dec 11 '19 at 00:34
  • 1
    @CynicalF you don't have to manually enter all the dates. You just need to pass the column to `isoweek` function but make sure the class of that column is "Date". `lubridate::isoweek(mydata$ddate)`. – Ronak Shah Dec 11 '19 at 00:55
  • @RonakShah Okay, thank you! That worked. – Fire Dec 11 '19 at 01:12

2 Answers2

2

We can use the %V argument of format (and other functions):

dat <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
     ddate     week
1  2015-1-2   0
2  2015-1-3   0
3  2015-1-10   1
4  2015-1-18   2")
dat$ddate <- as.Date(dat$ddate, format = "%Y-%m-%d")

dat$week2 <- format(dat$ddate, "%V")
dat$week3 <- as.integer(format(dat$ddate, "%V")) - 1L
dat
#        ddate week week2 week3
# 1 2015-01-02    0    01     0
# 2 2015-01-03    0    01     0
# 3 2015-01-10    1    02     1
# 4 2015-01-18    2    03     2

This assumes that ddate is actually stored as a Date object, though doing a transient conversion and back again is far better than any work-around that attempts to to this without being a Date.

As @Onyambu suggested, you might prefer "%W" (Monday as start-of-week) over "%V" (Sunday as start-of-week).

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • NO no consider using `%W` instead of `%V`. This is most probably a work related job thus days start from `Monday` – Onyambu Dec 11 '19 at 00:47
  • I have no idea why you think that assumption is safe. Or not. It could be either, so while it is not safe to assume Sunday or Monday, it is very helpful to know of the presence of both formats. Thanks. – r2evans Dec 11 '19 at 12:22
0

The solution by Ronak Shah, lubridate::isoweek(mydata$ddate), worked.

Fire
  • 301
  • 1
  • 2
  • 9