如何标记上周五或最后一天或月

时间:2016-06-07 15:52:02

标签: r date

我有一个日期和星期几的数据框

> head(data)
         day   weekday
1 2016-01-01    Friday
4 2016-01-04    Monday
5 2016-01-05   Tuesday
6 2016-01-06 Wednesday
7 2016-01-07  Thursday
8 2016-01-08    Friday

这是代码:

data <- data.frame(day = seq(as.Date("2014-01-01"), as.Date("2016-05-10"), "day"), 
                  weekday = weekdays(seq(as.Date("2014-01-01"), as.Date("2016-05-10"), 
                                         "day")))
excludeDays <- c("Saturday", "Sunday")
data <- subset(data, !weekdays(data$day) %in% excludeDays) #exclude weekend

我想添加2个标记每个月的最后一个星期五以及该月的最后一天的列,无论它是哪一天。你会怎么做?

更具体一点: 我希望它仅为我的数据确定日期是否是该月的最后一个日期。例如,如果我的数据在2016年1月的日期和1月的最后一个日期是2016年1月20日那么应该标记为该月的最后一天。同样,我想在我的数据中识别一个月的最后一个星期五,而不是在日历上。

4 个答案:

答案 0 :(得分:1)

进一步澄清并遵循@eminik的评论下面的代码

library(data.table)
setDT(data)
data[, LastDayInMonth := day == max(day), by = .(year(day), month(day))]
data[, LastFridayInMonth := weekday == "Friday" & day == max(day), 
     by = .(year(day), month(day), weekdays(day))]

产生

# show results (only relevant rows)
data[LastDayInMonth | LastFridayInMonth == TRUE]

          day  weekday LastDayInMonth LastFridayInMonth
1: 2016-01-29   Friday           TRUE              TRUE
2: 2016-02-26   Friday          FALSE              TRUE
3: 2016-02-29   Monday           TRUE             FALSE
4: 2016-03-25   Friday          FALSE              TRUE
5: 2016-03-31 Thursday           TRUE             FALSE
6: 2016-04-29   Friday           TRUE              TRUE
7: 2016-05-06   Friday          FALSE              TRUE
8: 2016-05-10  Tuesday           TRUE             FALSE

编辑:根据OP的要求修改代码以应对年份的变化。

注意: weekdays会在使用中的语言环境中返回名称​​的字符向量。因此,只有在使用英语语言环境时,代码才有效。否则,您可能必须先使用Sys.setlocale(category = "LC_ALL", locale = "English")

答案 1 :(得分:0)

我正在使用lubridate进行日期计算,但由于您错过了几天,我使用lead()中的dplyr函数来查找数据中的第二天(行)

library(lubridate)
library(dplyr)
my.data <- tbl_df(data)
my.data <- my.data %>% 
    # First last day of month
    # get the next day in the data
    mutate(next.day = lead(day)) %>%
    # it's month is different
    mutate(LastDayInMonth = ifelse(month(day) != month(next.day), T, F)) %>%
    # Now Last Friday
    mutate(LastFridayInMonth = 
               ifelse( (wday(day) == 6) & # It's a Friday
                # Check the month of next Friday, different
                (month(lead(day, 5)) != month(day)
                 | is.na(lead(day, 5))), T, F))

答案 2 :(得分:0)

library(lubridate)
library(dplyr)
data%>% 
  mutate(year = year(day),month= month(day)) %>%
  group_by(year, month) %>% 
  mutate(LastDayInMonth = max(day)==day)%>% 
  arrange(day) %>%
  ungroup() %>% 
  group_by(year, month, weekday) %>%
  mutate(LastWeekInMonth = row_number() ==  n(), 
         LastFriayInMonth = weekday =="Freitag" & LastWeekInMonth == 1) %>% 
  ungroup()%>%
  select(day, weekday, LastDayInMonth,LastFriayInMonth)

与上面相同的摘录(%&gt;%过滤器(LastDayInMonth | LastFriayInMonth == TRUE):

Source: local data frame [8 x 4]

         day    weekday LastDayInMonth LastFriayInMonth
      (date)     (fctr)          (lgl)            (lgl)
1 2016-01-29    Freitag           TRUE             TRUE
2 2016-02-26    Freitag          FALSE             TRUE
3 2016-02-29     Montag           TRUE            FALSE
4 2016-03-25    Freitag          FALSE             TRUE
5 2016-03-31 Donnerstag           TRUE            FALSE
6 2016-04-29    Freitag           TRUE             TRUE
7 2016-05-06    Freitag          FALSE             TRUE
8 2016-05-10   Dienstag           TRUE            FALSE

答案 3 :(得分:0)

[ null, { "equipment_type_id": 1, "total": 1 }, { "equipment_type_id": "2", "total": 10 } ]