嘿,谢谢您抽出一些时间来阅读我的问题。
我想做的是提取上个月的全部内容。以2018年9月(2018-09-01-2018-09-30)的月份为例,今天的日期为
Sys.Date() [1]“ 2018-10-05”
`lst %>%
mutate(taskDate = as.Date(taskDate)) %>%
filter(taskDate >= as.Date(Sys.Date() %m-% months(1), '%Y%m%d')) %>%
filter(taskDate != as.Date(Sys.Date(), 'month')) %>%
select('taskMinutes','taskBillable') %>%
group_by(taskBillable) %>%
summarise(total = sum(as.numeric(taskMinutes))) %>%
mutate(total = (total/60)) %>%
mutate(total= as.numeric(total))`
此代码获得下一个结果“ 2018-09-05”-“ 2018-10-05” 我想要的是上个月才是2018-09-01-2018-09-30
我该怎么办?
taskDate taskMinutes taskBillable
2018-09-24T00:00:00 180 FALSE
2018-09-24T00:00:00 390 TRUE
2018-09-24T00:00:00 540 FALSE
2018-09-21T00:00:00 60 TRUE
2018-09-24T00:00:00 30 FALSE
2018-09-24T00:00:00 30 FALSE
2018-09-24T00:00:00 120 TRUE
2018-09-25T00:00:00 390 TRUE
2018-09-25T00:00:00 480 TRUE
2018-09-21T00:00:00 240 TRUE
2018-09-21T00:00:00 240 FALSE
2018-09-24T00:00:00 60 TRUE
2018-09-25T00:00:00 60 TRUE
2018-09-26T00:00:00 30 TRUE
2018-09-26T00:00:00 480 FALSE
2018-09-24T00:00:00 240 TRUE
2018-09-25T00:00:00 240 TRUE
2018-09-27T00:00:00 360 TRUE
2018-09-27T00:00:00 150 TRUE
2018-09-27T00:00:00 480 TRUE
2018-09-27T00:00:00 90 TRUE
2018-09-26T00:00:00 60 TRUE
2018-09-26T00:00:00 240 TRUE
2018-09-27T00:00:00 180 TRUE
2018-10-02T00:00:00 30 TRUE
2018-10-02T00:00:00 60 FALSE
2018-10-02T00:00:00 540 FALSE
2018-10-02T00:00:00 480 TRUE
2018-09-28T00:00:00 180 TRUE
2018-09-27T00:00:00 15 FALSE
2018-09-28T00:00:00 15 FALSE
2018-09-28T00:00:00 120 TRUE
2018-09-28T00:00:00 30 TRUE
2018-09-27T00:00:00 210 TRUE
2018-09-28T00:00:00 30 FALSE
2018-10-03T00:00:00 60 TRUE
2018-09-28T00:00:00 480 TRUE
2018-10-01T00:00:00 480 TRUE
2018-10-01T00:00:00 456 FALSE
答案 0 :(得分:0)
您可以使用lubridate和filter,确保使用正确的> =,<运算符正确捕获整个月。
library(dplyr)
library(lubridate)
df <- read.table(text =
"taskDate taskMinutes taskBillable
2017-10-05T00:00:00 210 TRUE
2017-10-06T00:00:00 30 TRUE
2017-10-16T00:00:00 60 TRUE
2017-10-18T00:00:00 120 TRUE
2017-10-19T00:00:00 120 TRUE
2017-10-25T00:00:00 60 TRUE
2017-10-26T00:00:00 120 TRUE
2017-11-01T00:00:00 60 TRUE
2017-11-02T00:00:00 60 TRUE
2017-11-03T00:00:00 30 TRUE
2017-10-30T00:00:00 60 TRUE
2017-10-30T00:00:00 120 TRUE
2017-10-31T00:00:00 60 TRUE",
header = T,
stringsAsFactors = F)
# convert taskDate to date format
df$taskDate <- gsub("T", " ", df$taskDate) %>% ymd_hms()
str(df)
# 'data.frame': 13 obs. of 3 variables:
# $ taskDate : POSIXct, format: "2017-10-05" "2017-10-06" "2017-10-16" ...
# $ taskMinutes : int 210 30 60 120 120 60 120 60 60 30 ...
# $ taskBillable: logi TRUE TRUE TRUE TRUE TRUE TRUE ...
# filter 0 note the (- years(1) + months(1)) could be removed, but is needed for the sample data
filter(df,
taskDate >= floor_date(Sys.Date(), "month") - months(1) - years(1) + months(1),
taskDate < ceiling_date(Sys.Date(), "month") - months(1) - years(1) + months(1))
# taskDate taskMinutes taskBillable
# 1 2017-10-05 210 TRUE
# 2 2017-10-06 30 TRUE
# 3 2017-10-16 60 TRUE
# 4 2017-10-18 120 TRUE
# 5 2017-10-19 120 TRUE
# 6 2017-10-25 60 TRUE
# 7 2017-10-26 120 TRUE
# 8 2017-10-30 60 TRUE
# 9 2017-10-30 120 TRUE
# 10 2017-10-31 60 TRUE