BlackHat
BlackHat

Reputation: 755

R assign week value for a range of numbers

I have a data frame that looks like this:

dataset <- data.frame(date = seq(from=as.Date("2015-07-06"),
                                 to=as.Date("2015-07-15"),by="day"),
                      stringsAsFactors=F)

My objective is to assign a week value to a sequence first 5 dates so it looks something like this:

          date   week
 1: 2015-07-06 Week 1
 2: 2015-07-07 Week 1
 3: 2015-07-08 Week 1
 4: 2015-07-09 Week 1
 5: 2015-07-10 Week 1
 6: 2015-07-11 Week 2
 7: 2015-07-12 Week 2
 8: 2015-07-13 Week 2
 9: 2015-07-14 Week 2
10: 2015-07-15 Week 2

My data is only week day data, hence it's only 5 days. Each week starts from a Monday ...just to give some context.

Is there a way to do this besides counting first 5 and assigning "Week 1", then counting the next 5 and assigning "Week 2"...and so on?

I'm putting this piece in a for loop so I'm hoping for a straighforward solution.

Thank you very much!

Upvotes: 0

Views: 587

Answers (3)

akrun
akrun

Reputation: 887028

As the 'date' column have only week days and without any breaks, we can use gl/paste to create week index. This doesn't depend on the nrow of the dataset i.e. even if the nrow is not a multiple of 5, it will work.

 dataset$week <- paste('Week', as.numeric(gl(nrow(dataset),5, nrow(dataset))))

Other option would be using format after converting the 'date' column to 'Date' class.

format(as.Date(dataset$date),'%W')
#[1] "27" "27" "27" "27" "27" "27" "27" "28" "28" "28"

Or

week(strptime(dataset$date,format='%Y-%m-%d'))
#[1] 27 27 28 28 28 28 28 28 28 29

but, I am not sure that is what you wanted.

Upvotes: 2

RHertel
RHertel

Reputation: 23788

Here's a simple solution using base R:

nweeks <- 10 #choose as required
days <- paste0("Week",rep(seq(nweeks),each=5))
#> days
# [1] "Week1"  "Week1"  "Week1"  "Week1"  "Week1"  "Week2"  "Week2"  "Week2"  "Week2"  "Week2"  "Week3"  "Week3"  "Week3"  "Week3"  "Week3" 
#[16] "Week4"  "Week4"  "Week4"  "Week4"  "Week4"  "Week5"  "Week5"  "Week5"  "Week5"  "Week5"  "Week6"  "Week6"  "Week6"  "Week6"  "Week6" 
#[31] "Week7"  "Week7"  "Week7"  "Week7"  "Week7"  "Week8"  "Week8"  "Week8"  "Week8"  "Week8"  "Week9"  "Week9"  "Week9"  "Week9"  "Week9" 
#[46] "Week10" "Week10" "Week10" "Week10" "Week10"

Upvotes: 1

SabDeM
SabDeM

Reputation: 7190

Here is my solution withe the week function of the lubridate package. Note that before passing to the week function column date need to be converted with ymd as a POSIX class.

library(lubridate)
dataset$date <- ymd(dataset$date)
dataset$week <- week(dataset$date)
dataset
         date week
1  2015-07-06   27
2  2015-07-07   27
3  2015-07-08   28
4  2015-07-09   28
5  2015-07-10   28
6  2015-07-11   28
7  2015-07-12   28
8  2015-07-13   28
9  2015-07-14   28
10 2015-07-15   29

Upvotes: 2

Related Questions