Lyndz
Lyndz

Reputation: 423

Subsetting dates in R

I have the following data

dat <- structure(list(Datetime = structure(c(1261987200, 1261987500, 
1261987800, 1261988100, 1261988400), class = c("POSIXct", "POSIXt"
), tzone = ""), Rain = c(0, -999, -999, -999, -999)), row.names = c(NA, 
5L), class = "data.frame")

The first column contains the dates (year, month, day, hour). The second column is Rainfall.

The dates are not continuous. Some of the dates with missing Rainfall were already removed.

I would like to ask what is the best way of subsetting this data in terms of Year, Day, month or hour?

For example, I just want to get all data for July (month = 7). What I do is something like this:

dat$month<-substr(dat$Datetime,6,7)
july<-dat[which(dat$month == 7),]

or if its a year, say 2010:

dat$year<-substr(dat$Datetime,1,4)
dat<-which(dat$year == 2010),]

Then convert them into numeric types.

Is there an easier way to do this in R? the dates are already formatted using POSIXlt.

I'll appreciate any help on this.

Lyndz

Upvotes: 0

Views: 47

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101129

If you want to convert the Datetime to year or month (numeric), you can try format like below

df1 <- transform(
  df,
  year = as.numeric(format(Datetime,"%Y")),
  month = as.numeric(format(Datetime,"%m"))
)

which gives

             Datetime Rain year month
1 2009-12-28 09:00:00    0 2009    12
2 2009-12-28 09:05:00 -999 2009    12
3 2009-12-28 09:10:00 -999 2009    12
4 2009-12-28 09:15:00 -999 2009    12
5 2009-12-28 09:20:00 -999 2009    12

If you want to subset df1 further by year (for example, year == 2010), then

subset(
  df1,
  year == 2010
)

Upvotes: 2

Desmond
Desmond

Reputation: 1137

You can use the lubridate package and associated month and year functions.

library(tidyverse)
library(lubridate)

df <- structure(list(
  Datetime = structure(
    c(1261987200, 1261987500,
      1261987800, 1261988100, 1261988400),
    class = c("POSIXct", "POSIXt"),
    tzone = ""
  ),
  Rain = c(0,-999,-999,-999,-999)
),
row.names = c(NA,
              5L),
class = "data.frame") %>%
  as_tibble()

df %>% 
  mutate(month = lubridate::month(Datetime),
         year = lubridate::year(Datetime))

Output:

# A tibble: 5 x 4
  Datetime             Rain month  year
  <dttm>              <dbl> <dbl> <dbl>
1 2009-12-28 16:00:00     0    12  2009
2 2009-12-28 16:05:00  -999    12  2009
3 2009-12-28 16:10:00  -999    12  2009
4 2009-12-28 16:15:00  -999    12  2009
5 2009-12-28 16:20:00  -999    12  2009

Upvotes: 2

Related Questions