Grouping by date range

  • data in a vector:
vDates <- as.Date(c("2013-06-01", "2013-07-08", "2013-09-01", "2013-09-15"))
  • vector that sorts those by month
vDates.bymonth <- cut(vDates, breaks = "month")
  • combine these into a data frame:
dfDates <- data.frame(vDates, vDates.bymonth)

  • cut() function
  • group series of dates and associated values by date range such as

    • week
    • month
    • quarter
    • year
  • as.Date() function
    • convert string object as a date object.
    • convert between character representations and objects of class "Date" representing calendar dates.
  • data in a vector:

vDates <- as.Date(c("2013-06-01", "2013-07-08", "2013-09-01", "2013-09-15"))

# creates: 
# [1] "2013-06-01" "2013-07-08" "2013-09-01" "2013-09-15"
  • vector that sorts those by month
vDates.bymonth <- cut(vDates, breaks = "month")

# produces: 
# [1] 2013-06-01 2013-07-01 2013-09-01 2013-09-01
# Levels: 2013-06-01 2013-07-01 2013-08-01 2013-09-01
  • combine these into a data frame:
dfDates <- data.frame(vDates, vDates.bymonth)


Reference:

results matching ""

    No results matching ""