0

I have got a series of video uploads and I want to show how many videos has been uploaded in the last month, half year, year, 2, year, 3, year ...

PUBLISHED_AT is of type posixct and should be transformed into factor with the levels above.

Is there a nice way to do so? Seems like I am not the first person thinking about something like this.

The dates are like:

Date<- as.POSIXct(c("2015-12-11 00:00:01", "2016-01-11 00:00:01", "2014-01-11 00:00:01", "2015-12-11 00:00:01", "2016-04-04 08:22:01", "2013-12-11 00:00:01") 
                    , format= "%Y-%m-%d %H:%M:%S")
DF<- data.frame(Date, 
                Number=ceiling(abs(rnorm(1:6))))

I thought about using the cut-Function but i don't know how to specify the breaks

barracuda317
  • 608
  • 7
  • 24
  • Please provide the data / reproducible example. We also ask that you show some attempt you've made and the problem that you've run into. – Hack-R Sep 22 '16 at 14:54

3 Answers3

1

My approach would be like this:

PUBLISHED_AT <- as.POSIXct(c("2015-12-11 00:00:01") 
                       , format= "%Y-%m-%d %H:%M:%S")

library(lubridate)
half_year <- interval(start = ymd(Sys.Date() - months(6)), end = ymd(Sys.Date()), tzone = "ECST")
year <- interval(start = ymd(Sys.Date() - months(12)), end = ymd(Sys.Date()), tzone = "ECST")
two_years <- interval(start = ymd(Sys.Date() - months(24)), end = ymd(Sys.Date()), tzone = "ECST")

PUBLISHED_AT %within% c(half_year, year, two_years)
#[1] FALSE  TRUE TRUE

You can do this now for all dates and perhaps via lapply() or something else.

J_F
  • 9,956
  • 2
  • 31
  • 55
  • That is not what I was looking for, because the result is not a factor, but it lead me the right way. I answered with my approach – barracuda317 Sep 22 '16 at 15:49
1

Thanks to J_F I ended up with something like this:

library(lubridate)
oneMonth <- Sys.time() - ddays(30)
threeMonth <- Sys.time() - ddays(90)
sixMonth <- Sys.time() - ddays(180)
oneYear <-  Sys.time() - dyears(1)
twoYear <- Sys.time() - dyears(2)
threeYear<- Sys.time() - dyears(3)

cut(videos$PUBLISHED_AT, c(as.POSIXct(channel$PUBLISHED_AT), threeYear,twoYear,oneYear,sixMonth,threeMonth,oneMonth,Sys.time()))
barracuda317
  • 608
  • 7
  • 24
0

Does your data looks like this? Since you haven't updated a example

  Date<- as.POSIXct(c("2015-12-11 00:00:01", "2016-01-11 00:00:01", "2014-01-11 00:00:01", "2015-12-11 00:00:01", "2016-04-04 08:22:01", "2013-12-11 00:00:01") 
                        , format= "%Y-%m-%d %H:%M:%S")
    DF<- data.frame(Date, 
                    Number=ceiling(abs(rnorm(1:6))))
M. Kooi
  • 245
  • 3
  • 17
  • 1
    yes, that seems to be a good example. My data-range is from 2011-12-11 15:00:12 to 2016-09-22 10:00:00 I thought about using the `cut`-Function but i don't know how to specify the breaks – barracuda317 Sep 22 '16 at 15:09