我有一个data.frame,其中的日期列表存储在列中,而日期存储在另一列中。我只想按日期和日期属于该范围的日期和日期的日期过滤数据框。
例如:
dat1[format(dat1$date, '%m%d') %in% format(dat1$range, '%m%d'), ]
然后我尝试过滤
Source: local data frame [1 x 4]
Groups: <by row>
# A tibble: 6 x 4
start end range date
<date> <date> <list> <date>
1 2018-10-24 2018-10-29 <date [6]> 1998-10-17 ## as pointed out in comments
## this is incorrect. It would return an empty data.frame.
但是我得到一个错误:
prettyNum(.Internal(format(x,trim,digits,nsmall,width,3L,: 无效的“修剪”参数
我期望的地方
google-images-download
答案 0 :(得分:0)
您可以使用lubridate :: ymd重新进入。请注意,我必须更改用于采样日期的范围才能真正获得真实结果。
library(tidyverse)
library(lubridate)
set.seed(1)
data.frame(start = seq.Date(today() - 5, today(), 'day'),
end = seq.Date(today(), today() + 5, 'day')) %>%
mutate(date = sample(seq(as.Date('1998/10/15'), as.Date('1998/11/15'), by="day"), 6),
#using the year from start/end and the month/day from date, is date in the range?
InRange = ymd(year(start) * 10000 + month(date) * 100 + day(date)) >= start &
ymd(year(end) * 10000 + month(date) * 100 + day(date)) <= end) %>%
filter(InRange)
start end date InRange
1 2018-10-25 2018-10-30 1998-10-26 TRUE