# counting weeks in R

#### marwah

##### New Member
I have the following data

"2001-01-01" "2001-01-02" "2001-01-03" "2001-01-04" "2001-01-05"
"2001-01-06" "2001-01-07" "2001-01-08" "2001-01-09" "2001-01-10"
"2001-01-11" "2001-01-12" "2001-01-13" "2001-01-14" "2001-01-15"
"2001-01-16" "2001-01-17" "2001-01-18" "2001-01-19" "2001-01-20"
"2001-01-21" "2001-01-22" "2001-01-23" "2001-01-24" "2001-01-25"
"2001-01-26" "2001-01-27" "2001-01-28" "2001-01-29" "2001-01-30"
"2001-01-31" "2001-02-01" "2001-02-02" "2001-02-03" "2001-02-04"
"2001-02-05" "2001-02-06" "2001-02-07" "2001-02-08" "2001-02-09"

The date are as follows:
Code:
 dput(head(date,30))
structure(c(11323, 11324, 11325, 11326, 11327, 11328, 11329,
11330, 11331, 11332, 11333, 11334, 11335, 11336, 11337, 11338,
11339, 11340, 11341, 11342, 11343, 11344, 11345, 11346, 11347,
11348, 11349, 11350, 11351, 11352), class = "Date")
data are from 2001/01/01- 2020/12/31

I want to have an output looks like this:
Code:
month           week
Jan 2001          1
Jan 2001          2
Jan 2001          3
Jan 2001          4
Feb 2001          5
Feb 2001          6
....
Dec 2020         1044

Last edited:

#### Dason

You need to be more specific by what you mean. Once again example input and expected output would be a good place to start. I know this isn't homework but I'm going to post my typical homework help post thing here and please read though all of it and visit both links - then please be sure to follow the advice in the links:

Hi! :welcome: We are glad that you posted here! This looks like a homework question though. Our homework help policy can be found here. We mainly just want to see what you have tried so far and that you have put some effort into the problem. I would also suggest checking out this thread for some guidelines on smart posting behavior that can help you get answers that are better much more quickly.

#### marwah

##### New Member
You need to be more specific by what you mean. Once again example input and expected output would be a good place to start. I know this isn't homework but I'm going to post my typical homework help post thing here and please read though all of it and visit both links - then please be sure to follow the advice in the links:

Hi! :welcome: We are glad that you posted here! This looks like a homework question though. Our homework help policy can be found here. We mainly just want to see what you have tried so far and that you have put some effort into the problem. I would also suggest checking out this thread for some guidelines on smart posting behavior that can help you get answers that are better much more quickly.
Thank you but this is not a homework , it is my paper I'm working on and I need to count the number of weeks of the dates I have That is all

#### bryangoodrich

##### Probably A Mammal
Take your data and run dput on that vector of dates. Then paste the output of that here. We can use dget to import it directly back into R. As is, showing us those dates is useless.

But anyway, install package "lubridate" that has a nice function called "week" that returns the week number. Then you merely have to pass it your vector of dates and it'll return their week number. Use table on that return vector to get the counts of observations per week.

Code:
table(lubridate::week(x))

#### marwah

##### New Member
I tried the following
Code:
 library(ISOweek)
dates <- sub("(\\d{4}-)(\\d{2})", "\\1W\\2-1", date)
but it only gives weeks from 1-12 and what I want is to get the weeks separately and counts staring from 1 to 960

#### marwah

##### New Member
Am I right that from 2001/01/01 - 2020/12/31 has 960 weeks ?

#### marwah

##### New Member
Code:
 dput(head(date,20))
structure(c(11323, 11324, 11325, 11326, 11327, 11328, 11329,
11330, 11331, 11332, 11333, 11334, 11335, 11336, 11337, 11338,
11339, 11340, 11341, 11342), class = "Date")

#### Dason

One thing you should clarify is what you actually mean here. August 2, 2013 and August 5, 2013 are on two different calendar weeks but they aren't necessarily a week apart. What exactly do you mean to count?

#### marwah

##### New Member
One thing you should clarify is what you actually mean here. August 2, 2013 and August 5, 2013 are on two different calendar weeks but they aren't necessarily a week apart. What exactly do you mean to count?
I mean to aggregate the data from days to weeks and count the number of weeks i have for example:

I have week 1 as follows
2001/01/01 - 2001/01/07 starting from Monday- Sunday according to the calendar
and so on

#### bryangoodrich

##### Probably A Mammal
Code:
length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "week")) # 1044

#### marwah

##### New Member
Code:
length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "week")) # 1044
Thank you

#### Dason

Code:
length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "week")) # 1044
This doesn't count the unique calendar weeks though. This will give the number of week lengths that occur between the start and end date. But for example August 2, 2013 and August 5, 2013 fall on two different calendar weeks but this method will return the result "1" since they are within a week of each other.

I mean it could be what they want but it sounded to me like they wanted to count the number of calendar weeks in the data.

#### bryangoodrich

##### Probably A Mammal
The week function returns the week number since the start of the year.

This is how I'm thinking about the problem

Code:
somedates <-  seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day")  # The date range
x <- data.frame(
'date'   = somedates,
'year'   = year(somedates),
'week'   = week(somedates),
'values' = rnorm(length(somedates)))  # Made up data
y <- aggregate(values ~ year + week, x, sum)  # Total per week across years

#### marwah

##### New Member
The week function returns the week number since the start of the year.

This is how I'm thinking about the problem

Code:
somedates <-  seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day")  # The date range
x <- data.frame(
'date'   = somedates,
'year'   = year(somedates),
'week'   = week(somedates),
'values' = rnorm(length(somedates)))  # Made up data
y <- aggregate(values ~ year + week, x, sum)  # Total per week across years
Well thank you for the effort but what I want is to count the weeks in each month so 2001/01 has weeks from 1-4 , 2001/02 has weeks from 5-8 and so on

#### Dason

Can you explain how you're doing your counting? In my eye 2001/01 has weeks 1-5, 2001/02 has weeks 5-9 (week 5 is in both january and february...)

This is precisely why we ask for example input and desired output - we can't read your mind.

#### marwah

##### New Member
yes that is what I'm talking about sorry I don't know how the output would be as I don't know how to code it , if I know I won't ask !!
so yes January-2001, weeks from 1-5 Feb-2001 weeks from 5-9 , March-2001 weeks from 9-14 and so on
what I want will be something like this:
week:
1,2,3,4,5,6,7,8,9,10,.......,1044

#### Dason

yes that is what I'm talking about sorry I don't know how the output would be as I don't know how to code it , if I know I won't ask !!
This is wrong. Straight up wrong. You can't code if it if you don't know what the output should be -- not the other way around. A computer is a fast way to do calculations for you but you need to know what you want the computer to do for you. If you honestly can't explain to another human being what it is that you want then you have 0 chance of getting a computer to do it for you and you having any degree of confidence that it's the correct result.

You need to spend more time figuring out what it actually is that you want. This seems to be a big problem. You need to be able to clearly explain what you want to do. Once you have that then writing code becomes a lot easier - note that if you're familiar with the language or aren't a particularly competent programmer that it can still take a lot of work to tell the computer what you want to do ... but you have no hope of programming a solution if you don't even know what you want to do.

So before asking anybody else for help make sure that you read through those links we posted (you have read through those by now right? Dear god tell me you've read through them because if you've been posting this much and haven't given us the courtesy of reading through the guides we wrote to help you out then you're just wasting our time and I'll ask you to please not come back) and be able to adequately explain the problem you're having. If you put in a fair effort then we'll help you figure the rest out but remember that you're the one that wants help and you need to take steps in providing enough information so that other people can help you out. If you don't make it easy for us then why should we help you out?

#### marwah

##### New Member
This is wrong. Straight up wrong. You can't code if it if you don't know what the output should be -- not the other way around. A computer is a fast way to do calculations for you but you need to know what you want the computer to do for you. If you honestly can't explain to another human being what it is that you want then you have 0 chance of getting a computer to do it for you and you having any degree of confidence that it's the correct result.

You need to spend more time figuring out what it actually is that you want. This seems to be a big problem. You need to be able to clearly explain what you want to do. Once you have that then writing code becomes a lot easier - note that if you're familiar with the language or aren't a particularly competent programmer that it can still take a lot of work to tell the computer what you want to do ... but you have no hope of programming a solution if you don't even know what you want to do.

So before asking anybody else for help make sure that you read through those links we posted (you have read through those by now right? Dear god tell me you've read through them because if you've been posting this much and haven't given us the courtesy of reading through the guides we wrote to help you out then you're just wasting our time and I'll ask you to please not come back) and be able to adequately explain the problem you're having. If you put in a fair effort then we'll help you figure the rest out but remember that you're the one that wants help and you need to take steps in providing enough information so that other people can help you out. If you don't make it easy for us then why should we help you out?
Thank you for the info, I'm really reading them and I do appreciate your effort in explaining to me how to ask a question, I will try one more time and hopefully I can make it right this time

#### marwah

##### New Member
but please just be patient and please don't ask me not to come back again , you guys are really amazing in helping people like me , I will try one more time