-1

I have found many answer regarding the week number of a particular date. What I want is to get a week number for 2 years i.e for first year it will give 1 to 53 weeks and then keep the count from 53 only and should not start with 1 again. Is it possible in R?. Example data is shown below:

enter image description here

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • @akrun I added a picture of my data frame. I have used data1['week_num']<-lubridate::week(data1$Date) to add week column to my data frame. It's showing week number 1 and so on for the date '2015-01-01'. Now the description is clear to you? Please remove the downvote – shivanshu dhawan Aug 18 '16 at 06:17
  • dput? can you please help me how to use it? I can't find it anywhere – shivanshu dhawan Aug 18 '16 at 06:20

1 Answers1

3

We can use rep to add 53 to the vector ('vN2') after finding the number of observations for each year.

vN2 + rep(c(0, 53), tapply(vN2, cumsum(c(TRUE, diff(vN2) < 0)), FUN = length))

data

set.seed(24)
vN <- rep(1:53, sample(1:5, 53, replace=TRUE))
vN1 <- rep(1:53, sample(1:6, 53, replace=TRUE))
vN2 <- c(vN, vN1)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Thank you for this. :) @akrun – shivanshu dhawan Aug 18 '16 at 06:25
  • I did @akrun. may be some internet issues. Thanks again :) . But I don't know why I am getting downvotes for every questions I post here – shivanshu dhawan Aug 18 '16 at 06:29
  • 2
    @shivanshudhawan Thank you for that. One way to avoid getting downvotes is to read [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) before posting the question. Also, before posting questions, it is better to research online (google) to see if there are questions that are similar to yours, (if there are, no need for posting :-), or else, show your code i.e. your effort in solving it and also the expected output. – akrun Aug 18 '16 at 06:32
  • 1
    will keep this in mind for sure. Thanks again mate :) – shivanshu dhawan Aug 18 '16 at 06:34
  • @shivanshudhawan Can you post that as a new question? – akrun Aug 22 '16 at 08:53