barracuda317
barracuda317

Reputation: 638

Grouping posixct-data non-linear

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

Upvotes: 0

Views: 50

Answers (3)

barracuda317
barracuda317

Reputation: 638

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()))

Upvotes: 1

J_F
J_F

Reputation: 10372

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.

Upvotes: 1

M. Kooi
M. Kooi

Reputation: 255

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))))

Upvotes: 0

Related Questions