Home > Mobile >  Getting the last 6 months data in R
Getting the last 6 months data in R

Time:10-13

i had data frame which contain many row. Here it is:

library(lubridate)

date_ <- date(seq(ymd_h("2020-01-01-00"), ymd_h("2021-03-31-23"), by = "hours"))
hour_ <- hour(seq(ymd_h("2020-01-01-00"), ymd_h("2021-03-31-23"), by = "hours"))

game1 <- sort(round(runif(length(datex), 10, 50), 0), decreasing = TRUE)
game2 <- sort(round(runif(length(datex), 20, 100), 0), decreasing = TRUE)
game3 <- sort(round(runif(length(datex), 30, 150), 0), decreasing = TRUE)
game4 <- sort(round(runif(length(datex), 40, 200), 0), decreasing = TRUE)

game_data <- data.frame(date_, hour_, game1, game2, game3, game4)

I just want to subset game_data to get all the last 6 months data. How do i get it?

CodePudding user response:

Last 6 months of data from the max date in the data ?

You can try -

library(lubridate)
library(dplyr)

result <- subset(game_data, date_ > max(date_) %m-% months(6))

Or with dplyr -

result <- game_data %>% filter(date_ > max(date_) %m-% months(6))
  • Related