0

I have a dataframe Events that looks like this

EventCount      Date
3317       2015-01-05
3388       2015-01-12
3467       2015-01-19
3455       2015-01-26
3506       2015-02-02
3561       2015-02-09

What I want is to create a new column 'EventType', based on which week the event took place. Those that took place before 2015-01-12 are of type A, between 2015-01-12 and 2015-02-02 are of type B and after 2015-02-02 are of type C.

What I tried is using the ifelse condition to create a new column.

  Events$EventType<-ifelse(Events$Date < as.Date('2015-02-02'),"B","C")

This gives me a new column that categorizes by only two conditions, and not three.

user2510479
  • 1,528
  • 13
  • 17
  • Take a look at [this link](https://stackoverflow.com/questions/12379128/r-switch-statement-on-comparisons). The answer is something you could use. – steveb Sep 22 '17 at 03:08
  • Also [Nested ifelse statement](https://stackoverflow.com/questions/18012222/nested-ifelse-statement-in-r) can be helpful. – Ronak Shah Sep 22 '17 at 03:10

3 Answers3

2

By using cut

dt['EventType']=cut(dt$Date,breaks=as.Date(c('2000-01-01','2015-01-12','2015-02-02','2111-01-01')),labels = c('A','B','C'))

 dt
  EventCount       Date EventType
1       3317 2015-01-05         A
2       3388 2015-01-12         B
3       3467 2015-01-19         B
4       3455 2015-01-26         B
5       3506 2015-02-02         C
6       3561 2015-02-09         C

As per thelatemail

cut(dat$Date, c(as.Date(c("2015-01-12","2015-02-02")), c(-Inf,Inf)), labels=1:3)
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 2
    To get around using the large and small dates, you can also employ `Inf` and `-Inf`: `cut(dat$Date, c(as.Date(c("2015-01-12","2015-02-02")), c(-Inf,Inf)), labels=LETTERS[1:3])` – thelatemail Sep 22 '17 at 03:29
  • @thelatemail thank you so much , I was looking for it , save my day ~ :) – BENY Sep 22 '17 at 03:33
  • 1
    The trick is making sure you end up with a `Date` object, which means you can't do the more intuitive `c(-Inf, as.Date(c("2015-01-12","2015-02-02")), Inf)` but rather `c(as.Date(c("2015-01-12","2015-02-02")), c(-Inf,Inf))`. The latter shows `Inf` values as `NA` when printed, but they are actually stored as `Inf` – thelatemail Sep 22 '17 at 03:35
  • @thelatemail this make a lot of sense!:)I will mark it done – BENY Sep 22 '17 at 03:36
  • Any idea as to this error: Error in cut.POSIXt(meanAOD$DateTime, breaks = as.Date(c("2000-01-01", : invalid specification of 'breaks' – Melanie Baker Jul 21 '22 at 14:18
  • 1
    @MelanieBaker cut.POSIXt , should it be cut ? – BENY Jul 21 '22 at 15:15
0

In practice (and perhaps hurry) you might do it in two commands in the following way:

Events$EventType <- ifelse(Events$Date < as.Date("2015-01-12"), "A", "B")
Events$EventType[Events$Date > "2015-02-02"] <- "C" 

The data:

Events <- data.frame(EventCount = c(3317L, 3388L, 3467L, 3455L, 3506L, 3561L),
                     Date       = c("2015-01-05", "2015-01-12", "2015-01-19",
                                    "2015-01-26", "2015-02-02", "2015-02-09"))
Events$Date <- as.Date(Events$Date)
s_baldur
  • 29,441
  • 4
  • 36
  • 69
0

Here is my attempt using a function with sapply

setEventType <- function(date){
  if(date < as.Date("2015-01-12")){
    return("A")
  }
  else if(date >= as.Date("2015-02-02")){
      return("C")
  }
  else{
     return("B")
  }
}

Events$EventType <- sapply(as.Date(Events$Date), setEventType)

Output:

 EventCount       Date EventType
1       3317 2015-01-05         A
2       3388 2015-01-12         B
3       3467 2015-01-19         B
4       3455 2015-01-26         B
5       3506 2015-02-02         C
6       3561 2015-02-09         C
Imran Ali
  • 2,223
  • 2
  • 28
  • 41